From 24125aa7679d701cddc9f64b34f8efae3fc1ecf8 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Wed, 24 Jul 2024 19:15:02 -0300 Subject: [PATCH] feat(oauth): token getter method --- internals/oauth/oauth.templ | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internals/oauth/oauth.templ b/internals/oauth/oauth.templ index cc854d0..3e720c1 100644 --- a/internals/oauth/oauth.templ +++ b/internals/oauth/oauth.templ @@ -14,6 +14,7 @@ import ( type OAuthClient interface { ServeHTTP(w http.ResponseWriter, r *http.Request) + Token(r *http.Request) (string, error) LoginButton() templ.Component } @@ -146,3 +147,23 @@ func (c DefaultOAuthClient) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + +func (c DefaultOAuthClient) Token(r *http.Request) (string, error) { + cookie, err := r.Cookie("__Host-OAUTH-" + strings.ToUpper(c.Name)) + if err != nil { + return "", e.Join(e.New("Unable get token cookie"), err) + } + + j, err := url.PathUnescape(cookie.Value) + if err != nil { + return "", e.Join(e.New("Unable to unescape token json"), err) + } + + var token DefaultOAuthToken + err = json.Unmarshal([]byte(j), &token) + if err != nil { + return "", e.Join(e.New("Unable to parse token json"), err) + } + + return token.Token, nil +}