This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbitbucket.go
71 lines (57 loc) · 2.38 KB
/
bitbucket.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
package auth
import (
"net/http"
)
type BitbucketUser struct {
UserId string `json:"username"`
UserPicture string `json:"avatar"`
UserLastName string `json:"last_name"`
UserFirstName string `json:"first_name"`
}
func (u *BitbucketUser) Id() string { return u.UserId }
func (u *BitbucketUser) Provider() string { return "bitbucket.org" }
func (u *BitbucketUser) Name() string { return u.UserId }
func (u *BitbucketUser) Email() string { return "" }
func (u *BitbucketUser) Link() string { return "https://bitbucket.org/"+u.UserId }
func (u *BitbucketUser) Picture() string { return u.UserPicture }
func (u *BitbucketUser) Org() string { return "" }
// BitbucketProvider is an implementation of Bitbucket's Oauth1.0a protocol.
// See https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket
type BitbucketProvider struct {
OAuth1Mixin
}
// NewBitbucketProvider allocates and returns a new BitbucketProvider.
func NewBitbucketProvider(key, secret, callback string) *BitbucketProvider {
bb := BitbucketProvider{}
//bb.AuthorizationURL = "https://bitbucket.org/!api/1.0/oauth/authenticate"
//bb.RequestTokenURL = "https://bitbucket.org/api/1.0/oauth/request_token/"
//bb.AccessTokenURL = "https://bitbucket.org/api/1.0/oauth/access_token/"
bb.AuthorizationURL = "https://bitbucket.org/api/1.0/oauth/authenticate/"
bb.RequestTokenURL = "https://bitbucket.org/api/1.0/oauth/request_token/"
bb.AccessTokenURL = "https://bitbucket.org/api/1.0/oauth/access_token/"
bb.CallbackURL = callback
bb.ConsumerKey = key
bb.ConsumerSecret = secret
return &bb
}
// GetAuthenticatedUser will upgrade the oauth_token to an access token, and
// invoke the appropriate Bitbucket REST API call to get the User's information.
func (self *BitbucketProvider) GetAuthenticatedUser(w http.ResponseWriter, r *http.Request) (User, Token, error) {
// upgrade the oauth_token to an access token
token, err := self.OAuth1Mixin.AuthorizeToken(w, r)
if err != nil {
return nil, nil, err
}
// bitbuckets user object comes wrapped in a composite object.
wrapper := struct {
User *BitbucketUser `json:"user"`
}{}
// get the Bitbucket User details
if err := self.OAuth1Mixin.GetAuthenticatedUser("https://api.bitbucket.org/1.0/user", token, &wrapper); err != nil {
return nil, nil, err
}
if wrapper.User == nil {
//TODO throw an exception
}
return wrapper.User, token, nil
}