-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_provider.go
98 lines (84 loc) · 2.7 KB
/
auth_provider.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
86
87
88
89
90
91
92
93
94
95
96
97
98
package auth
import (
"io/ioutil"
"github.com/pikomonde/pikopos/common"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
// GenerateStateAndGetAuthURL is used to generate state and get Auth URL
func (s *ServiceAuth) GenerateStateAndGetAuthURL(provider string) (string, string, error) {
state := common.RandomBase64()
config, err := s.repositoryAuth.GetAuthConfig(provider)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
}).Errorln("[ServiceAuth][GenerateStateAndGetAuthURL][GetAuthConfig]: ", err.Error())
return "", "", err
}
return state, config.AuthCodeURL(state), nil
}
// Exchange is used to get token from provider using provider's code
func (s *ServiceAuth) Exchange(provider, code string) (*oauth2.Token, error) {
config, err := s.repositoryAuth.GetAuthConfig(provider)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
"code": code,
}).Errorln("[ServiceAuth][Exchange][GetAuthConfig]: ", err.Error())
return nil, err
}
// getting token
ctx, cancel := common.ContextWithDuration()
defer cancel()
token, err := config.Exchange(ctx, code)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
"code": code,
}).Errorln("[ServiceAuth][Exchange][Exchange]: ", err.Error())
return nil, err
}
return token, nil
}
// GetIDFromProvider is used to get ID from provider.
func (s *ServiceAuth) GetIDFromProvider(provider string, token *oauth2.Token) (string, error) {
config, err := s.repositoryAuth.GetAuthConfig(provider)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
}).Errorln("[ServiceAuth][GetIDFromProvider][GetAuthConfig]: ", err.Error())
return "", err
}
// getting client
ctx, cancel := common.ContextWithDuration()
defer cancel()
client := config.Client(ctx, token)
// getting userinfo
resp, err := client.Get(providers[provider].userinfoURL)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
"userinfoURL": providers[provider].userinfoURL,
}).Errorln("[ServiceAuth][GetIDFromProvider][Get UserInfo]: ", err.Error())
return "", err
}
defer resp.Body.Close()
// unmarshall response
userinfoBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
"userinfoURL": providers[provider].userinfoURL,
}).Errorln("[ServiceAuth][GetIDFromProvider][ReadAll]: ", err.Error())
return "", err
}
idFromProvider, err := providers[provider].respToID(userinfoBytes)
if err != nil {
log.WithFields(log.Fields{
"provider": provider,
"userinfoURL": providers[provider].userinfoURL,
}).Errorln("[ServiceAuth][GetIDFromProvider][respToID]: ", err.Error())
return "", err
}
return idFromProvider, nil
}