[JWT Auth] Support JWT Authentication#13
Open
harnish-crest-data wants to merge 12 commits into
Open
Conversation
Author
|
The PR is ready for review. Also, this will be in testing once Aliabbas is done with salesforce testing. |
kush-elastic
suggested changes
Jun 26, 2024
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
| @@ -300,11 +306,21 @@ func GetConnectedCount() int { | |||
| } | |||
|
|
|||
| func GetSalesforceCredentials(ap AuthenticationParameters) (creds *Credentials, err error) { | |||
Contributor
There was a problem hiding this comment.
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
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
github.com/golang-jwt/jwtlibrary.How to test this PR?
./examplesdir.main.go.go run main.go.Related issues