This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
/
slack.go
61 lines (50 loc) · 1.57 KB
/
slack.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
package drivers
import (
"io/ioutil"
"net/http"
"github.com/danilopolani/gocialite/structs"
"golang.org/x/oauth2/slack"
)
const slackDriverName = "slack"
func init() {
registerDriver(slackDriverName, SlackDefaultScopes, SlackUserFn, slack.Endpoint, SlackAPIMap, SlackUserMap)
}
// SlackUserMap is the map to create the User struct
var SlackUserMap = map[string]string{
"real_name": "FullName",
"first_name": "FirstName",
"last_name": "LastName",
"email": "Email",
"image_original": "Avatar",
}
// SlackAPIMap is the map for API endpoints
var SlackAPIMap = map[string]string{
"endpoint": "https://slack.com/api",
"userEndpoint": "/users.profile.get",
"authEndpoint": "/auth.test",
}
// SlackUserFn is a callback to parse additional fields for User
var SlackUserFn = func(client *http.Client, u *structs.User) {
// Get user ID
req, err := client.Get(SlackAPIMap["endpoint"] + SlackAPIMap["authEndpoint"])
if err != nil {
return
}
defer req.Body.Close()
res, _ := ioutil.ReadAll(req.Body)
data, err := jsonDecode(res)
if err != nil {
return
}
u.ID = data["user_id"].(string)
// Fetch other user information
userInfo := u.Raw["profile"].(map[string]interface{})
u.Username = userInfo["display_name"].(string)
u.FullName = userInfo["real_name"].(string)
u.FirstName = userInfo["first_name"].(string)
u.LastName = userInfo["last_name"].(string)
u.Email = userInfo["email"].(string)
u.Avatar = userInfo["image_original"].(string)
}
// SlackDefaultScopes contains the default scopes
var SlackDefaultScopes = []string{"users.profile:read"}