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

Method HandleToken

pkg/web/oauthclient/token.go:58–96  ·  view source on GitHub ↗

HandleToken is a handler that returns a valid OAuth token. It reads the token from the authorization cookie and refreshes it if needed. If the authorization cookie is not there, it returns a 401 Unauthorized error.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

56// It reads the token from the authorization cookie and refreshes it if needed.
57// If the authorization cookie is not there, it returns a 401 Unauthorized error.
58func (oc *OAuthClient) HandleToken(w http.ResponseWriter, r *http.Request) {
59 value, err := oc.getAuthCookie(w, r)
60 if err != nil {
61 webhandlers.Error(w, r, err)
62 return
63 }
64
65 currentToken := &oauth2.Token{
66 AccessToken: value.AccessToken,
67 RefreshToken: value.RefreshToken,
68 Expiry: time.Now(),
69 }
70
71 freshToken, err := oc.Token(r.Context(), currentToken)
72 if err != nil {
73 webhandlers.Error(w, r, err)
74 return
75 }
76
77 if freshToken != currentToken {
78 err = oc.setAuthCookie(w, r, authCookie{
79 AccessToken: freshToken.AccessToken,
80 RefreshToken: freshToken.RefreshToken,
81 Expiry: freshToken.Expiry,
82 })
83 if err != nil {
84 webhandlers.Error(w, r, err)
85 return
86 }
87 }
88
89 webhandlers.JSON(w, r, struct {
90 AccessToken string `json:"access_token"`
91 Expiry time.Time `json:"expiry"`
92 }{
93 AccessToken: freshToken.AccessToken,
94 Expiry: freshToken.Expiry,
95 })
96}

Callers

nothing calls this directly

Calls 7

getAuthCookieMethod · 0.95
TokenMethod · 0.95
setAuthCookieMethod · 0.95
ErrorFunction · 0.92
JSONFunction · 0.92
NowMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected