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
|
package auth
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/alexedwards/scs/v2"
"golang.org/x/oauth2"
)
var (
profileURL = "https://www.googleapis.com/oauth2/v3/userinfo"
)
// Handler authentication handler
// handles, login, oauth and registration
type Handler interface {
Login(w http.ResponseWriter, r *http.Request)
OauthCallback(w http.ResponseWriter, r *http.Request)
}
type authHandler struct {
service Servicer
ses *scs.SessionManager
oclient *oauth2.Config
}
// NewHandler create new instance of handler
// Servicer, session, and oauth client required
func NewHandler(service Servicer,
session *scs.SessionManager,
oclient *oauth2.Config) Handler {
return &authHandler{
service,
session,
oclient,
}
}
func (h *authHandler) Login(w http.ResponseWriter, r *http.Request) {
stateValue, err := h.service.GenerateStateToken()
if err != nil {
return
}
h.ses.Put(r.Context(), "state", stateValue)
url := h.oclient.AuthCodeURL(stateValue, oauth2.AccessTypeOnline)
http.Redirect(w, r, url, 302)
}
func (h *authHandler) OauthCallback(w http.ResponseWriter, r *http.Request) {
// this needs to be random
stateFromSession := h.ses.GetString(r.Context(), "state")
stateFromCallback := r.FormValue("state")
// prevent arbitrary authorization exchanges
if stateFromCallback != stateFromSession {
http.Error(w, "state value miss match bad data", 400)
return
}
stateValid, err := h.service.ValidateStateToken(stateFromCallback, stateFromSession)
if err != nil {
http.Error(w, "error validating", 400)
return
}
if !stateValid {
http.Error(w, "invalid tokens", 400)
return
}
// valid and same as state
code := r.FormValue("code")
token, err := h.oclient.Exchange(r.Context(), code)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
log.Printf("returned token %v", token)
// google profile
client := h.oclient.Client(r.Context(), token)
resp, err := client.Get(profileURL)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
gp := &GoogleAuthProfile{}
err = json.Unmarshal(data, &gp)
profileID, newProfile, err := h.service.LoginOrRegisterSessionID(token, gp)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
h.ses.Put(r.Context(), "profid", profileID)
// send to registration
if newProfile == true {
http.Redirect(w, r, "/account/register", 302)
return
}
http.Redirect(w, r, "/", 302)
}
|