-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add azure oidc PKI auth instead of client secret #9054
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8ba23b1
feat: add azure oidc PKI auth instead of client secret
Emyrk c62e548
add client cert and key as deployment options
Emyrk 664895f
add unit test
Emyrk 74f9245
fixup! add unit test
Emyrk ce39f2d
Refactor code into a method
Emyrk 7346fbf
Custom token refresher to handle pki auth
Emyrk 9c73346
Add unit test for mock e2e
Emyrk 15db163
Add comments
Emyrk 801799f
add little comment about constant 5min
Emyrk 6914619
Fix typo
Emyrk e6c0a57
Merge remote-tracking branch 'origin/main' into stevenmasley/pki_auth…
Emyrk cb98e18
update golden files
Emyrk 1479644
Test linting
Emyrk 0f508f5
Add yaml config options
Emyrk 189007f
make gen again...
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: add azure oidc PKI auth instead of client secret
- Loading branch information
commit 8ba23b1ebcc5bd64636a17fc047bbf06deba5ccd
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package azureauth | ||
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"crypto/sha1" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/pem" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
// JWTAssertion is used by Azure AD when doing OIDC with a PKI cert instead of | ||
// a client secret. | ||
// https://learn.microsoft.com/en-us/azure/active-directory/develop/certificate-credentials | ||
type JWTAssertion struct { | ||
*oauth2.Config | ||
|
||
// ClientSecret is the private key of the PKI cert. | ||
// Azure AD only supports RS256 signing algorithm. | ||
clientKey *rsa.PrivateKey | ||
// Base64url-encoded SHA-1 thumbprint of the X.509 certificate's DER encoding. | ||
x5t string | ||
} | ||
|
||
func NewJWTAssertion(config *oauth2.Config, pemEncodedKey string, pemEncodedCert string) (*JWTAssertion, error) { | ||
rsaKey, err := DecodeKeyCertificate([]byte(pemEncodedKey)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
block, _ := pem.Decode([]byte(pemEncodedCert)) | ||
hashed := sha1.Sum(block.Bytes) | ||
|
||
return &JWTAssertion{ | ||
Config: config, | ||
clientKey: rsaKey, | ||
x5t: base64.StdEncoding.EncodeToString(hashed[:]), | ||
}, nil | ||
} | ||
|
||
// DecodeKeyCertificate decodes a PEM encoded PKI cert. | ||
func DecodeKeyCertificate(pemEncoded []byte) (*rsa.PrivateKey, error) { | ||
block, _ := pem.Decode(pemEncoded) | ||
key, err := x509.ParsePKCS1PrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to parse private key: %w", err) | ||
} | ||
|
||
return key, nil | ||
} | ||
|
||
func (ja *JWTAssertion) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string { | ||
return ja.Config.AuthCodeURL(state, opts...) | ||
} | ||
|
||
func (ja *JWTAssertion) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { | ||
now := time.Now() | ||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{ | ||
"aud": ja.Config.Endpoint.TokenURL, | ||
"exp": now.Add(time.Minute * 5).Unix(), | ||
"jti": uuid.New().String(), | ||
"nbf": now.Unix(), | ||
"iat": now.Unix(), | ||
|
||
// TODO: Should be app GUID, not client ID. | ||
"iss": ja.Config.ClientID, | ||
"sub": ja.Config.ClientID, | ||
}) | ||
token.Header["x5t"] = ja.x5t | ||
|
||
signed, err := token.SignedString(ja.clientKey) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to sign jwt assertion: %w", err) | ||
} | ||
|
||
opts = append(opts, | ||
oauth2.SetAuthURLParam("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"), | ||
oauth2.SetAuthURLParam("client_assertion", signed), | ||
) | ||
return ja.Config.Exchange(ctx, code, opts...) | ||
} | ||
|
||
func (ja *JWTAssertion) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.TokenSource { | ||
return ja.Config.TokenSource(ctx, token) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.