forked from dghubble/gologin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
85 lines (77 loc) · 3 KB
/
login.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
package bitbucket
import (
"errors"
"net/http"
"github.com/rohenaz/gologin/v2"
oauth2Login "github.com/rohenaz/gologin/v2/oauth2"
"golang.org/x/oauth2"
)
// Bitbucket login errors
var (
ErrUnableToGetBitbucketUser = errors.New("bitbucket: unable to get Bitbucket User")
)
// StateHandler checks for a state cookie. If found, the state value is read
// and added to the ctx. Otherwise, a non-guessable value is added to the ctx
// and to a (short-lived) state cookie issued to the requester.
//
// Implements OAuth 2 RFC 6749 10.12 CSRF Protection. If you wish to issue
// state params differently, write a http.Handler which sets the ctx state,
// using oauth2 WithState(ctx, state) since it is required by LoginHandler
// and CallbackHandler.
func StateHandler(config gologin.CookieConfig, success http.Handler) http.Handler {
return oauth2Login.StateHandler(config, success)
}
// LoginHandler handles Bitbucket login requests by reading the state value
// from the ctx and redirecting requests to the AuthURL with that state value.
func LoginHandler(config *oauth2.Config, failure http.Handler) http.Handler {
return oauth2Login.LoginHandler(config, failure)
}
// CallbackHandler handles Bitbucket redirection URI requests and adds the
// Bitbucket access token and User to the ctx. If authentication succeeds,
// handling delegates to the success handler, otherwise to the failure
// handler.
func CallbackHandler(config *oauth2.Config, success, failure http.Handler) http.Handler {
success = bitbucketHandler(config, success, failure)
return oauth2Login.CallbackHandler(config, success, failure)
}
// bitbucketHandler is a http.Handler that gets the OAuth2 Token from the ctx
// to get the corresponding Bitbucket User. If successful, the User is added to
// the ctx and the success handler is called. Otherwise, the failure handler is
// called.
func bitbucketHandler(config *oauth2.Config, success, failure http.Handler) http.Handler {
if failure == nil {
failure = gologin.DefaultFailureHandler
}
fn := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
token, err := oauth2Login.TokenFromContext(ctx)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(w, req.WithContext(ctx))
return
}
httpClient := config.Client(ctx, token)
bitbucketClient := newClient(httpClient)
user, resp, err := bitbucketClient.CurrentUser()
err = validateResponse(user, resp, err)
if err != nil {
ctx = gologin.WithError(ctx, err)
failure.ServeHTTP(w, req.WithContext(ctx))
return
}
ctx = WithUser(ctx, user)
success.ServeHTTP(w, req.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
// validateResponse returns an error if the given Bitbucket User, raw
// http.Response, or error are unexpected. Returns nil if they are valid.
func validateResponse(user *User, resp *http.Response, err error) error {
if err != nil || resp.StatusCode != http.StatusOK {
return ErrUnableToGetBitbucketUser
}
if user == nil || user.Username == "" {
return ErrUnableToGetBitbucketUser
}
return nil
}