-
Notifications
You must be signed in to change notification settings - Fork 25
feat: Support workload identity federation flow #4074
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 all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
7317bd8
feat: Remove refresh_token grant type
JorTurFer 5c170fa
Update changelogs
JorTurFer b5d425c
update exp time of assertion as access token CAN'T be longer that it
JorTurFer f1f2aa5
Update changelogs
JorTurFer c397af6
feat: Support Workload Identity Federation flow
JorTurFer 16c49fd
update changelog
JorTurFer 882e49d
apply feedback
JorTurFer cf8ba2b
apply feedback
JorTurFer 3c2a3f5
apply feedback
JorTurFer b848c8f
apply feedback
JorTurFer f3021f8
remove docs from PR
JorTurFer 92b147e
remove docs from PR
JorTurFer c9ba9c6
remove docs from PR
JorTurFer 6d1635d
remove docs from PR
JorTurFer a55aaf7
add static token
JorTurFer d7fe988
fix linting issues
JorTurFer c7cd630
fix panic
JorTurFer ff22cbd
replace wif assertion options with a func
JorTurFer ff91d49
apply feedback
JorTurFer f09b995
apply feedback
JorTurFer 95bad8d
move GH requester to SDK
JorTurFer 8808690
move GH requester to SDK
JorTurFer a987bfa
Update core/clients/auth_flow.go
JorTurFer e619bb0
Update deprecation message
JorTurFer 94694e9
Update deprecation message
JorTurFer 7c0b874
restore refresh token code
JorTurFer 1400132
update comment
JorTurFer 037d062
Apply feedback
JorTurFer 70433a7
Apply feedback
JorTurFer 6dca0d3
.
JorTurFer 06aa8d2
make getBackgroundTokenRefreshContext private
JorTurFer e29f1d3
Update envs
JorTurFer 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
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 |
|---|---|---|
|
|
@@ -234,4 +234,4 @@ See the [release documentation](./RELEASE.md) for further information. | |
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 | ||
| Apache 2.0 | ||
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| v0.20.1 | ||
| v0.21.0 |
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
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,88 @@ | ||
| package clients | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultTokenExpirationLeeway = time.Second * 5 | ||
| ) | ||
|
|
||
| type AuthFlow interface { | ||
| RoundTrip(req *http.Request) (*http.Response, error) | ||
| GetAccessToken() (string, error) | ||
| getBackgroundTokenRefreshContext() context.Context | ||
| refreshAccessToken() error | ||
| } | ||
|
|
||
| // TokenResponseBody is the API response | ||
| // when requesting a new token | ||
| type TokenResponseBody struct { | ||
| AccessToken string `json:"access_token"` | ||
| ExpiresIn int `json:"expires_in"` | ||
JorTurFer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Deprecated: RefreshToken is no longer used and the SDK will not attempt to refresh tokens using it but will instead use the AuthFlow implementation to get new tokens. | ||
| // This will be removed after 2026-07-01. | ||
| RefreshToken string `json:"refresh_token"` | ||
| Scope string `json:"scope"` | ||
| TokenType string `json:"token_type"` | ||
| } | ||
|
|
||
| func parseTokenResponse(res *http.Response) (*TokenResponseBody, error) { | ||
| if res == nil { | ||
| return nil, fmt.Errorf("received bad response from API") | ||
| } | ||
| if res.StatusCode != http.StatusOK { | ||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| // Fail silently, omit body from error | ||
| // We're trying to show error details, so it's unnecessary to fail because of this err | ||
| body = []byte{} | ||
| } | ||
| return nil, &oapierror.GenericOpenAPIError{ | ||
| StatusCode: res.StatusCode, | ||
| Body: body, | ||
| } | ||
| } | ||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| token := &TokenResponseBody{} | ||
| err = json.Unmarshal(body, token) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unmarshal token response: %w", err) | ||
| } | ||
| return token, nil | ||
| } | ||
|
|
||
| func tokenExpired(token string, tokenExpirationLeeway time.Duration) (bool, error) { | ||
| if token == "" { | ||
| return true, nil | ||
| } | ||
|
|
||
| // We can safely use ParseUnverified because we are not authenticating the user at this point. | ||
| // We're just checking the expiration time | ||
| tokenParsed, _, err := jwt.NewParser().ParseUnverified(token, &jwt.RegisteredClaims{}) | ||
| if err != nil { | ||
| return false, fmt.Errorf("parse token: %w", err) | ||
| } | ||
|
|
||
| expirationTimestampNumeric, err := tokenParsed.Claims.GetExpirationTime() | ||
| if err != nil { | ||
| return false, fmt.Errorf("get expiration timestamp: %w", err) | ||
| } | ||
|
|
||
| // Pretend to be `tokenExpirationLeeway` into the future to avoid token expiring | ||
| // between retrieving the token and upstream systems validating it. | ||
| now := time.Now().Add(tokenExpirationLeeway) | ||
| return now.After(expirationTimestampNumeric.Time), nil | ||
| } | ||
Oops, something went wrong.
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.