-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
94 lines (78 loc) · 2.02 KB
/
auth.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
package auth
import (
"net/http"
"errors"
"time"
"golang.org/x/oauth2"
"github.com/gorilla/sessions"
"github.com/gorilla/securecookie"
)
const WriteEmailScope = "write:email"
const WriteImageScope = "write:image"
const WriteMemberScope = "write:member"
const ReadMemberScope = "read:member"
const ModifyEntryScope = "modify:entry"
var providers = []string{"google", "github"}
// store will hold all session data
var store *sessions.CookieStore
type AuthHandler struct {
Store *sessions.CookieStore
}
func New() AuthHandler {
authKeyOne := securecookie.GenerateRandomKey(64)
encryptionKeyOne := securecookie.GenerateRandomKey(32)
store = sessions.NewCookieStore(
authKeyOne,
encryptionKeyOne,
)
store.Options = &sessions.Options{
Path: "/",
MaxAge: 3600 * 8,
HttpOnly: true,
}
return AuthHandler{Store: store}
}
func (a AuthHandler) CheckScope(router http.HandlerFunc, scope string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !a.ScopeOk(w, r, ReadMemberScope) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
router(w, r)
}
}
func (auth AuthHandler) getToken(w http.ResponseWriter, r *http.Request) (*oauth2.Token, error) {
session, err := auth.Store.Get(r, "auth-session")
if err != nil {
return nil, err
}
for _, provider := range providers {
val := session.Values[provider]
// var token = &oauth2.Token{}
if token, ok := val.(*oauth2.Token); ok {
if token.Expiry.Before(time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)) || token.Expiry.After(time.Now()) {
return token, nil
}
}
}
return nil, errors.New("Token not found")
}
func (auth AuthHandler) ScopeOk(w http.ResponseWriter, r *http.Request, scope string) bool {
session, err := auth.Store.Get(r, "auth-session")
if err != nil {
return false
}
_, err = auth.getToken(w, r)
if err != nil {
return false
}
val := session.Values["scopes"]
if scopes, ok := val.([]string); ok {
for _, s := range scopes {
if s == scope {
return true
}
}
}
return false
}