forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraincube.go
74 lines (66 loc) · 1.61 KB
/
braincube.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
package providers
import (
"errors"
"fmt"
"net/http"
"net/url"
"github.com/bitly/oauth2_proxy/api"
)
type BraincubeProvider struct {
*ProviderData
}
func NewBraincubeProvider(p *ProviderData) *BraincubeProvider {
p.ProviderName = "braincube"
fmt.Println("Braincube provider")
if p.LoginURL == nil || p.LoginURL.String() == "" {
p.LoginURL = &url.URL{
Scheme: "https",
Host: "mybraincube.com",
Path: "/sso-server/ws/oauth2/authorize.jsp",
}
}
if p.RedeemURL == nil || p.RedeemURL.String() == "" {
p.RedeemURL = &url.URL{
Scheme: "https",
Host: "mybraincube.com",
Path: "/sso-server/ws/oauth2/token",
}
}
if p.ProfileURL == nil || p.ProfileURL.String() == "" {
p.ProfileURL = &url.URL{
Scheme: "https",
Host: "mybraincube.com",
Path: "/sso-server/ws/oauth2/me",
}
}
if p.Scope == "" {
p.Scope = "BASE"
}
return &BraincubeProvider{ProviderData: p}
}
func getBraincubeHeader(AccessToken string) http.Header {
header := make(http.Header)
header.Set("Accept", "application/json")
header.Set("Authorization", fmt.Sprintf("Bearer %s", AccessToken))
return header
}
func (p *BraincubeProvider) GetEmailAddress(s *SessionState) (string, error) {
if s.AccessToken == "" {
return "", errors.New("missing access token")
}
req, err := http.NewRequest("GET", p.ProfileURL.String(), nil)
if err != nil {
return "", err
}
req.Header = getBraincubeHeader(s.AccessToken)
json, err := api.Request(req)
if err != nil {
return "", err
}
email, err := json.Get("userEmail").String()
if err != nil {
fmt.Printf("Error: %s\n", err)
return "", err
}
return email, nil
}