-
Notifications
You must be signed in to change notification settings - Fork 335
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
Support oauth2 authentication for pulsar-client-go #313
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a12a7c8
Authentication provider for OAuth 2.0
EronWright 97d98f7
Add tests and Update license
zymap 2f836e2
Revert zstd version
zymap b7c3cfc
Refactor to support multiple issuers.
EronWright d7955db
Address comments
zymap a83c9c8
Add tests
zymap 0d2b678
Merge branch 'eron-oauth2' into eron-oauth2-with-multi-issuer
zymap 2ab51ac
Merge pull request #1 from streamnative/eron-oauth2-with-multi-issuer
zymap c49fb40
Change clock package
zymap 10845df
Merge branch 'eron-oauth2' of https://github.com/zymap/pulsar-client-…
zymap ceeee07
Merge remote-tracking branch 'upstream/master' into eron-oauth2
zymap 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
Authentication provider for OAuth 2.0
- based on cloud-cli @ bc645b16ca7b7474b132ee1da8b56da35025a616
- Loading branch information
commit a12a7c83813546c3a0f0800fd3c2c2a567426474
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,96 @@ | ||
// Copyright (c) 2020 StreamNative, Inc.. All Rights Reserved. | ||
|
||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"golang.org/x/oauth2" | ||
"k8s.io/utils/clock" | ||
) | ||
|
||
const ( | ||
ClaimNameUserName = "https://streamnative.io/username" | ||
) | ||
|
||
// Flow abstracts an OAuth 2.0 authentication and authorization flow | ||
type Flow interface { | ||
// Authorize obtains an authorization grant based on an OAuth 2.0 authorization flow. | ||
// The method returns a grant which may contain an initial access token. | ||
Authorize() (*AuthorizationGrant, error) | ||
} | ||
|
||
// AuthorizationGrantRefresher refreshes OAuth 2.0 authorization grant | ||
type AuthorizationGrantRefresher interface { | ||
// Refresh refreshes an authorization grant to contain a fresh access token | ||
Refresh(grant *AuthorizationGrant) (*AuthorizationGrant, error) | ||
} | ||
|
||
type AuthorizationGrantType string | ||
|
||
const ( | ||
// GrantTypeClientCredentials represents a client credentials grant | ||
GrantTypeClientCredentials AuthorizationGrantType = "client_credentials" | ||
|
||
// GrantTypeDeviceCode represents a device code grant | ||
GrantTypeDeviceCode AuthorizationGrantType = "device_code" | ||
) | ||
|
||
// AuthorizationGrant is a credential representing the resource owner's authorization | ||
// to access its protected resources, and is used by the client to obtain an access token | ||
type AuthorizationGrant struct { | ||
// Type describes the type of authorization grant represented by this structure | ||
Type AuthorizationGrantType `json:"type"` | ||
|
||
// ClientCredentials is credentials data for the client credentials grant type | ||
ClientCredentials *KeyFile `json:"client_credentials,omitempty"` | ||
|
||
// Token contains an access token in the client credentials grant type, | ||
// and a refresh token in the device authorization grant type | ||
Token *oauth2.Token `json:"token,omitempty"` | ||
} | ||
|
||
// TokenResult holds token information | ||
type TokenResult struct { | ||
AccessToken string `json:"access_token"` | ||
IDToken string `json:"id_token"` | ||
RefreshToken string `json:"refresh_token"` | ||
ExpiresIn int `json:"expires_in"` | ||
} | ||
|
||
// Issuer holds information about the issuer of tokens | ||
type Issuer struct { | ||
IssuerEndpoint string | ||
ClientID string | ||
Audience string | ||
} | ||
|
||
func convertToOAuth2Token(token *TokenResult, clock clock.Clock) oauth2.Token { | ||
return oauth2.Token{ | ||
AccessToken: token.AccessToken, | ||
TokenType: "bearer", | ||
RefreshToken: token.RefreshToken, | ||
Expiry: clock.Now().Add(time.Duration(token.ExpiresIn) * time.Second), | ||
} | ||
} | ||
|
||
// ExtractUserName extracts the username claim from an authorization grant | ||
func ExtractUserName(token oauth2.Token) (string, error) { | ||
p := jwt.Parser{} | ||
claims := jwt.MapClaims{} | ||
if _, _, err := p.ParseUnverified(token.AccessToken, claims); err != nil { | ||
return "", fmt.Errorf("unable to decode the access token: %v", err) | ||
} | ||
username, ok := claims[ClaimNameUserName] | ||
if !ok { | ||
return "", fmt.Errorf("access token doesn't contain a username claim") | ||
} | ||
switch v := username.(type) { | ||
case string: | ||
return v, nil | ||
default: | ||
return "", fmt.Errorf("access token contains an unsupported username claim") | ||
} | ||
} |
This file contains 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,44 @@ | ||
// Copyright (c) 2020 StreamNative, Inc.. All Rights Reserved. | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAuth(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "cloud-cli Auth Suite") | ||
} | ||
|
||
type MockTokenExchanger struct { | ||
CalledWithRequest interface{} | ||
ReturnsTokens *TokenResult | ||
ReturnsError error | ||
RefreshCalledWithRequest *RefreshTokenExchangeRequest | ||
} | ||
|
||
func (te *MockTokenExchanger) ExchangeCode(req AuthorizationCodeExchangeRequest) (*TokenResult, error) { | ||
te.CalledWithRequest = &req | ||
return te.ReturnsTokens, te.ReturnsError | ||
} | ||
|
||
func (te *MockTokenExchanger) ExchangeRefreshToken(req RefreshTokenExchangeRequest) (*TokenResult, error) { | ||
te.RefreshCalledWithRequest = &req | ||
return te.ReturnsTokens, te.ReturnsError | ||
} | ||
|
||
func (te *MockTokenExchanger) ExchangeClientCredentials(req ClientCredentialsExchangeRequest) (*TokenResult, error) { | ||
te.CalledWithRequest = &req | ||
return te.ReturnsTokens, te.ReturnsError | ||
} | ||
|
||
func (te *MockTokenExchanger) ExchangeDeviceCode(ctx context.Context, | ||
req DeviceCodeExchangeRequest) (*TokenResult, error) { | ||
te.CalledWithRequest = &req | ||
return te.ReturnsTokens, te.ReturnsError | ||
} |
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.
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.
This dependency was due to the mock clock right? I recall that you forked it elsewhere, would that work here too?
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.
Yes. Let me replace it.