This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
/
bitbucket.go
53 lines (42 loc) · 1.4 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
package drivers
import (
"io/ioutil"
"net/http"
"github.com/danilopolani/gocialite/structs"
"golang.org/x/oauth2/bitbucket"
)
const bitbucketDriverName = "bitbucket"
func init() {
registerDriver(bitbucketDriverName, BitbucketDefaultScopes, BitbucketUserFn, bitbucket.Endpoint, BitbucketAPIMap, BitbucketUserMap)
}
// BitbucketUserMap is the map to create the User struct
var BitbucketUserMap = map[string]string{
"account_id": "ID",
"username": "Username",
"display_name": "FullName",
}
// BitbucketAPIMap is the map for API endpoints
var BitbucketAPIMap = map[string]string{
"endpoint": "https://api.bitbucket.org",
"userEndpoint": "/2.0/user",
"emailEndpoint": "/2.0/user/emails",
}
// BitbucketUserFn is a callback to parse additional fields for User
var BitbucketUserFn = func(client *http.Client, u *structs.User) {
// Set avatar
u.Avatar = u.Raw["links"].(map[string]interface{})["avatar"].(map[string]interface{})["href"].(string)
// Retrieve email
req, err := client.Get(BitbucketAPIMap["endpoint"] + BitbucketAPIMap["emailEndpoint"])
if err != nil {
return
}
defer req.Body.Close()
res, _ := ioutil.ReadAll(req.Body)
data, err := jsonDecode(res)
if err != nil {
return
}
u.Email = data["values"].([]interface{})[0].(map[string]interface{})["email"].(string)
}
// BitbucketDefaultScopes contains the default scopes
var BitbucketDefaultScopes = []string{"account", "email"}