-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
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
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,69 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/tsuru/go-tsuruclient/pkg/config" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func NewOIDCTokenSource(tokenV2 *config.TokenV2) oauth2.TokenSource { | ||
baseTokenSource := tokenV2.OAuth2Config.TokenSource(context.Background(), tokenV2.OAuth2Token) | ||
return newTokenSourceFSStorage(baseTokenSource, tokenV2) | ||
} | ||
|
||
type TokenSourceFSStorage struct { | ||
BaseTokenSource oauth2.TokenSource | ||
LastToken *config.TokenV2 | ||
} | ||
|
||
var _ oauth2.TokenSource = &TokenSourceFSStorage{} | ||
|
||
func (t *TokenSourceFSStorage) Token() (*oauth2.Token, error) { | ||
newToken, err := t.BaseTokenSource.Token() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !reflect.DeepEqual(t.LastToken.OAuth2Token, newToken) { | ||
fmt.Fprintf(os.Stderr, "The OIDC token was refreshed and expiry in %s\n", time.Since(newToken.Expiry)*-1) | ||
|
||
t.LastToken.OAuth2Token = newToken | ||
err = config.WriteTokenV2(*t.LastToken) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not write refreshed token: %s\n", err.Error()) | ||
return nil, err | ||
} | ||
|
||
err = config.WriteTokenV1(newToken.AccessToken) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not write legacy refreshed token: %s\n", err.Error()) | ||
return nil, err | ||
} | ||
} | ||
|
||
return newToken, nil | ||
} | ||
|
||
func newTokenSourceFSStorage(baseTokenSource oauth2.TokenSource, tokenV2 *config.TokenV2) oauth2.TokenSource { | ||
return &TokenSourceFSStorage{ | ||
BaseTokenSource: baseTokenSource, | ||
LastToken: tokenV2, | ||
} | ||
} | ||
|
||
type OIDCTokenProvider struct { | ||
OAuthTokenSource oauth2.TokenSource | ||
} | ||
|
||
func (ts *OIDCTokenProvider) Token() (string, error) { | ||
t, err := ts.OAuthTokenSource.Token() | ||
if err != nil { | ||
return "", err | ||
} | ||
return t.AccessToken, nil | ||
} |
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,51 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/tsuru/go-tsuruclient/pkg/config" | ||
tsuruerr "github.com/tsuru/tsuru/errors" | ||
) | ||
|
||
var errUnauthorized = &tsuruerr.HTTP{Code: http.StatusUnauthorized, Message: "unauthorized"} | ||
|
||
type TokenV1RoundTripper struct { | ||
http.RoundTripper | ||
} | ||
|
||
func (v *TokenV1RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
roundTripper := v.RoundTripper | ||
if roundTripper == nil { | ||
roundTripper = http.DefaultTransport | ||
} | ||
|
||
if token, err := config.ReadTokenV1(); err == nil && token != "" { | ||
req.Header.Set("Authorization", "bearer "+token) | ||
} | ||
|
||
response, err := roundTripper.RoundTrip(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if response.StatusCode == http.StatusUnauthorized { | ||
if teamToken := config.ReadTeamToken(); teamToken != "" { | ||
fmt.Fprintln(os.Stderr, "Invalid session - maybe invalid defined token on TSURU_TOKEN envvar") | ||
} else { | ||
fmt.Fprintln(os.Stderr, "Invalid session") | ||
} | ||
|
||
return nil, errUnauthorized | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func NewTokenV1RoundTripper() http.RoundTripper { | ||
return &TokenV1RoundTripper{ | ||
RoundTripper: http.DefaultTransport, | ||
} | ||
} |