MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / Token

Method Token

pkg/web/oauthclient/token.go:32–53  ·  view source on GitHub ↗

Token returns the OAuth 2.0 token. If the given token is about to expire, this method refreshes the token and returns the new token.

(ctx context.Context, token *oauth2.Token)

Source from the content-addressed store, hash-verified

30// Token returns the OAuth 2.0 token.
31// If the given token is about to expire, this method refreshes the token and returns the new token.
32func (oc *OAuthClient) Token(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
33 conf, err := oc.oauthConfig(ctx)
34 if err != nil {
35 return nil, err
36 }
37 ctx, err = oc.withHTTPClient(ctx)
38 if err != nil {
39 return nil, err
40 }
41 freshToken, err := conf.TokenSource(ctx, token).Token()
42 if err != nil {
43 var retrieveError *oauth2.RetrieveError
44 if stderrors.As(err, &retrieveError) {
45 var ttnErr errors.Error
46 if decErr := ttnErr.UnmarshalJSON(retrieveError.Body); decErr == nil {
47 return nil, errRefresh.WithCause(&ttnErr)
48 }
49 }
50 return nil, errRefresh.WithCause(err)
51 }
52 return freshToken, nil
53}
54
55// HandleToken is a handler that returns a valid OAuth token.
56// It reads the token from the authorization cookie and refreshes it if needed.

Callers 2

HandleLogoutMethod · 0.95
HandleTokenMethod · 0.95

Calls 5

withHTTPClientMethod · 0.95
UnmarshalJSONMethod · 0.95
TokenMethod · 0.65
AsMethod · 0.45
WithCauseMethod · 0.45

Tested by

no test coverage detected