/
requirement.go
69 lines (56 loc) · 1.36 KB
/
requirement.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
package oauth2
import (
"bytes"
"github.com/kataras/iris/core/errors"
"golang.org/x/oauth2"
"text/template"
)
type OAuth struct {
ProviderName string
Endpoint oauth2.Endpoint
ConfigHelpTemplate string
config Config
GatAccount AccountGetter
}
const requirementName = "oauth2"
func (o *OAuth) Name() string {
return requirementName
}
func (o *OAuth) Init(providerName string) {
o.ProviderName = providerName
}
func (o *OAuth) ConfigHelp() string {
wr := bytes.NewBufferString("")
template.Must(template.New("help_text").Parse(o.ConfigHelpTemplate)).Execute(wr, o)
return wr.String()
}
func (o *OAuth) Config() interface{} {
return o.config
}
func (o *OAuth) SetConfig(c interface{}) error {
conf, ok := c.(Config)
if !ok {
return errors.New("OAuth2 config has incorrect type.")
}
o.config = conf
return nil
}
func (o *OAuth) Fulfilled() bool {
return o.config.ClientID != "" && o.config.Secret != ""
}
func (o *OAuth) GoConfig(scopes []string) oauth2.Config {
return oauth2.Config{
Endpoint: o.Endpoint,
ClientID: o.config.ClientID,
ClientSecret: o.config.Secret,
Scopes: scopes,
RedirectURL: o.RedirectURL(),
}
}
func (o *OAuth) RedirectURL() string {
return "/oauth/callback/" + o.ProviderName
}
type Config struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
}