-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
112 lines (95 loc) · 2.7 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"github.com/dgrijalva/jwt-go"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"time"
)
var conf *oauth2.Config
var googleEndpoint = oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
type Assets struct {
Js string `json:"bundle.js"`
Css string `json:"style.css"`
}
type Page struct {
Title string
GoogleLoginUrl string
State string
Assets Assets
}
type User struct {
Subject string `json:"subject"`
Name string `json:"name"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified string `json:"emailVerified"`
}
func initAuth() {
conf = &oauth2.Config{
ClientID: GOOGLE_CLIENT_ID,
ClientSecret: GOOGLE_CLIENT_SECRET,
RedirectURL: GOOGLE_REDIRECT_URL,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
// You have to select your own scope from here -> https://developers.google.com/identity/protocols/googlescopes#google_sign-in
},
Endpoint: googleEndpoint,
}
}
func login(w http.ResponseWriter, r *http.Request) {
token := randToken()
googleUrl := conf.AuthCodeURL(token)
renderTemplate(w, "./webapp/login", &Page{
Title: "login",
GoogleLoginUrl: googleUrl,
})
}
func randToken() string {
b := make([]byte, 32)
rand.Read(b)
return base64.StdEncoding.EncodeToString(b)
}
func auth(w http.ResponseWriter, r *http.Request) {
authCode, err := conf.Exchange(oauth2.NoContext, r.URL.Query().Get("code")) //mux.Vars(r)["token"]
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
client := conf.Client(oauth2.NoContext, authCode)
email, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer email.Body.Close()
data, _ := ioutil.ReadAll(email.Body)
var user User
json.Unmarshal(data, &user)
expireToken := time.Now().Add(time.Hour * 24).Unix()
expireCookie := time.Now().Add(time.Hour * 24)
claims := Claims{
user.Email,
user.Picture,
jwt.StandardClaims{
ExpiresAt: expireToken,
Issuer: "MAD",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, _ := token.SignedString([]byte(JWT_SECRET_KEY))
cookie := http.Cookie{Name: "at", Value: signedToken, Expires: expireCookie, HttpOnly: true}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/documentlist", 307)
}
func logout(w http.ResponseWriter, r *http.Request) {
cookie := http.Cookie{Name: "at", Value: "", Expires: time.Now(), HttpOnly: true}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/login", 307)
}