-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth0_auth_context.go
145 lines (115 loc) · 3.16 KB
/
auth0_auth_context.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package authproxy
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"golang.org/x/oauth2"
)
type Auth0AuthContext struct {
Config *Configuration
AuthDomain string
ClientID string
ClientSecret string
CallbackURL string
ValidAccessTokens map[string]string
HTMLFile []byte
}
func NewAuth0AuthContext(config *Configuration) *Auth0AuthContext {
return &Auth0AuthContext{
Config: config,
ClientID: GetenvOrDie("AUTH0_CLIENT_ID"),
ClientSecret: GetenvOrDie("AUTH0_CLIENT_SECRET"),
AuthDomain: GetenvOrDie("AUTH0_DOMAIN"),
CallbackURL: GetenvOrDie("AUTH0_CALLBACK_URL"),
ValidAccessTokens: map[string]string{},
}
}
func (c *Auth0AuthContext) IsAccessTokenValidAndUserAuthorized(accessToken string) bool {
_, ok := c.ValidAccessTokens[accessToken]
if ok {
return true
}
return false
}
func (c *Auth0AuthContext) GetUserName(accessToken string) string {
username, _ := c.ValidAccessTokens[accessToken]
return username
}
func (c *Auth0AuthContext) GetHTTPEndpointPrefix() string {
return "/callback"
}
func (c *Auth0AuthContext) GetCookieName() string {
return "auth0_token"
}
func (c *Auth0AuthContext) ServeHTTP(w http.ResponseWriter, req *http.Request) {
conf := &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.CallbackURL,
Scopes: []string{"openid", "profile"},
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("https://%s/authorize", c.AuthDomain),
TokenURL: fmt.Sprintf("https://%s/oauth/token", c.AuthDomain),
},
}
code := req.URL.Query().Get("code")
token, err := conf.Exchange(oauth2.NoContext, code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
accessToken := token.AccessToken
expiration := time.Now().Add(24 * time.Hour)
cookie := http.Cookie{
Name: c.GetCookieName(),
Value: accessToken,
Expires: expiration,
}
http.SetCookie(w, &cookie)
client := conf.Client(oauth2.NoContext, token)
resp, err := client.Get(fmt.Sprintf("https://%s/userinfo", c.AuthDomain))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
raw, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var profile map[string]interface{}
if err = json.Unmarshal(raw, &profile); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var username string
username = profile["email"].(string)
usernames := MapUserNames(c.Config.Users, func(user interface{}) string {
return user.(User).Username
})
fmt.Println(username)
fmt.Println(usernames)
if inArray(username, usernames) {
c.ValidAccessTokens[accessToken] = username
}
http.Redirect(w, req, "/", 302)
}
func (c *Auth0AuthContext) GetLoginPage() ([]byte, error) {
return c.HTMLFile, nil
}
func (c *Auth0AuthContext) RenderHTMLFile() error {
tplBytes, err := publicAuth0HtmlTplBytes()
if err != nil {
return err
}
tpl := string(tplBytes)
f, err := RenderTemplate(tpl, c)
if err != nil {
return err
}
c.HTMLFile = f
return nil
}