OAuth

/app/lib/auth/provider.go (882 B)

 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
package auth

import (
"fmt"
"strings"

"github.com/markbates/goth"

"{{{ .Package }}}/app/util"
)

type Provider struct {
ID string `json:"id"`
Title string `json:"title"`
Key string `json:"-"`
Secret string `json:"-"`
Scopes []string `json:"-"`
}

func (p *Provider) Goth(proto string, host string) (goth.Provider, error) {
if p := util.GetEnv("oauth_protocol"); p != "" {
proto = p
}
if proto == "" {
proto = "http"
}
u := fmt.Sprintf("%s://%s", proto, host)

if env := util.GetEnv(util.AppKey + "_oauth_redirect"); env != "" {
u = env
}
if env := util.GetEnv("oauth_redirect"); env != "" {
u = env
}
u = strings.TrimSuffix(u, "/")
cb := fmt.Sprintf("%s/auth/callback/%s", u, p.ID)
gothPrv, err := toGoth(p.ID, p.Key, p.Secret, cb, p.Scopes...)
if err != nil {
return nil, err
}
goth.UseProviders(gothPrv)
return gothPrv, nil
}