-
Notifications
You must be signed in to change notification settings - Fork 301
/
callback.go
108 lines (94 loc) · 3.3 KB
/
callback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oauthclient
import (
"encoding/json"
stderrors "errors"
"net/http"
echo "github.com/labstack/echo/v4"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"golang.org/x/oauth2"
)
var (
errRefused = errors.DefinePermissionDenied("refused", "refused by OAuth server", "reason")
errNoStateParam = errors.DefinePermissionDenied("no_state", "no state parameter present in request")
errNoCodeParam = errors.DefinePermissionDenied("no_code", "no code parameter present in request")
errInvalidState = errors.DefinePermissionDenied("invalid_state", "invalid state parameter")
errExchange = errors.DefinePermissionDenied("exchange", "token exchange refused")
)
// HandleCallback is a handler that takes the auth code and exchanges it for the
// access token.
func (oc *OAuthClient) HandleCallback(c echo.Context) error {
if e := c.QueryParam("error"); e != "" {
return errRefused.WithAttributes("reason", c.QueryParam("error_description"))
}
state := c.QueryParam("state")
if state == "" {
return errNoStateParam.New()
}
code := c.QueryParam("code")
if code == "" {
return errNoCodeParam.New()
}
stateCookie, err := oc.getStateCookie(c)
if err != nil {
// Running the callback without state cookie often occurs when re-running
// the callback after successful token exchange (e.g. using the browser's
// back button after logging in). If there is a valid auth cookie, we just
// redirect back to the client mount instead of showing an error.
value, err := oc.getAuthCookie(c)
if err == nil && value.AccessToken != "" {
config := oc.configFromContext(c.Request().Context())
return c.Redirect(http.StatusFound, config.RootURL)
}
return err
}
if stateCookie.Secret != state {
return errInvalidState.New()
}
// Exchange token.
ctx, err := oc.withTLSClientConfig(c.Request().Context())
if err != nil {
return err
}
conf, err := oc.oauth(c)
if err != nil {
return err
}
token, err := conf.Exchange(ctx, code)
if err != nil {
var retrieveError *oauth2.RetrieveError
if stderrors.As(err, &retrieveError) {
var ttnErr errors.Error
if decErr := json.Unmarshal(retrieveError.Body, &ttnErr); decErr == nil {
return errExchange.WithCause(ttnErr)
}
}
return errExchange.WithCause(err)
}
oc.removeStateCookie(c)
err = oc.setAuthCookie(c, authCookie{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
Expiry: token.Expiry,
})
if err != nil {
return err
}
return oc.callback(c, token, stateCookie.Next)
}
func (oc *OAuthClient) defaultCallback(c echo.Context, _ *oauth2.Token, next string) error {
config := oc.configFromContext(c.Request().Context())
return c.Redirect(http.StatusFound, config.RootURL+next)
}