-
Notifications
You must be signed in to change notification settings - Fork 5
[JWT Auth] Support JWT Authentication #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[JWT Auth] Support JWT Authentication #13
Conversation
|
The PR is ready for review. Also, this will be in testing once Aliabbas is done with salesforce testing. |
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
c07e037 to
f21ba1f
Compare
bayeux.go
Outdated
| return st.connectCount | ||
| } | ||
|
|
||
| func GetSalesforceCredentials(ap AuthenticationParameters) (creds *Credentials, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve the structure and maintainability of the authentication methods in your Go code, I'll refactor the existing methods to separate the concerns of generating authentication parameters and fetching Salesforce credentials. This will make it easier to add new authentication methods in the future without modifying the existing codebase significantly. You can also add validations in each authentication method.
Here's the refactored code:
// GetJWTAuthentication prepares the authentication parameters for JWT-based authentication
func GetJWTAuthentication(clientId, username, audience, path string) (*Authentication, error) {
claims := jwt.MapClaims{
"iss": clientId,
"sub": username,
"aud": audience,
"exp": time.Now().Add(1 * time.Hour).Unix(),
}
privateKey, err := loadPrivateKey(path)
if err != nil {
return nil, err
}
tokenString, err := jwt.NewWithClaims(jwt.SigningMethodRS256, claims).SignedString(privateKey)
if err != nil {
return nil, err
}
return &Authentication{
urlValues: &url.Values{
"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"},
"assertion": {tokenString},
},
authParameters: &AuthenticationParameters{
ClientID: clientId,
Username: username,
Audience: audience,
Path: path,
},
}, nil
}
// GetClientCredentialAuthentication prepares the authentication parameters for client credential-based authentication
func GetClientCredentialAuthentication(clientId, clientSecret, username, password, tokenUrl string) (*Authentication, error) {
return &Authentication{
urlValues: &url.Values{
"grant_type": {"password"},
"client_id": {clientId},
"client_secret": {clientSecret},
"username": {username},
"password": {password},
},
authParameters: &AuthenticationParameters{
ClientID: clientId,
ClientSecret: clientSecret,
Username: username,
Password: password,
TokenURL: tokenUrl,
},
}, nil
}
// GetSalesforceCredentials fetches the Salesforce credentials using the prepared authentication parameters
func (a *Authentication) GetSalesforceCredentials() (creds *Credentials, err error) {
res, err := http.PostForm(a.authParameters.TokenURL, *a.urlValues)
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&creds); err != nil {
return nil, err
} else if creds.AccessToken == "" {
return nil, fmt.Errorf("unable to fetch access token: %w", err)
}
return creds, nil
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated thanks!
kush-elastic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What does this PR do?
github.com/golang-jwt/jwtlibrary.How to test this PR?
./examplesdir.main.go.go run main.go.Related issues