-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
140 lines (130 loc) · 4.03 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
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
package handlers
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/sessions"
asm "github.com/iochti/auth-service/models"
pb "github.com/iochti/auth-service/proto"
"github.com/iochti/gateway-service/helpers"
usm "github.com/iochti/user-service/models"
userpb "github.com/iochti/user-service/proto"
)
// AuthHandler wrapper
type AuthHandler struct {
AuthSvc pb.AuthSvcClient
UserSvc userpb.UserSvcClient
Store sessions.Store
ContentType string
}
func randToken() string {
b := make([]byte, 32)
rand.Read(b)
return base64.StdEncoding.EncodeToString(b)
}
// HandleLoginURLRequest returns the URL for the user to login
func (a *AuthHandler) HandleLoginURLRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", a.ContentType)
w.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:4200")
w.Header().Set("Access-Control-Allow-Credentials", "true")
ctx := helpers.GetContext(r)
state := randToken()
session, err := a.Store.Get(r, "state")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if session.Values["auth"] != nil {
userMsg, fetchErr := a.UserSvc.GetUser(ctx, &userpb.UserRequest{Categ: "login", Value: session.Values["auth"].(string)})
if fetchErr != nil {
http.Error(w, fetchErr.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(userMsg.GetUser()))
return
}
session.Values["state"] = state
rsp, err := a.AuthSvc.GetLoginURL(ctx, &pb.LoginURLRequest{State: state})
if err != nil {
fmt.Println("authsvc error :", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
bytesResp, err := json.Marshal(struct {
ConnectionURL string `json:"connection_url"`
}{rsp.GetUrl()})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write(bytesResp)
}
// HandleAuth handles GET:/auth request
func (a *AuthHandler) HandleAuth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", a.ContentType)
w.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:4200")
w.Header().Set("Access-Control-Allow-Credentials", "true")
ctx := helpers.GetContext(r)
session, err := a.Store.Get(r, "state")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
retrievedState := session.Values["state"]
// Get params
params := r.URL.Query()
state := params.Get("state")
code := params.Get("code")
if retrievedState != state {
http.Error(w, fmt.Errorf("Expected response state to match stored state").Error(), http.StatusInternalServerError)
return
}
rsp, err := a.AuthSvc.HandleAuth(ctx, &pb.AuthRequest{State: state, Code: code})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ghubUser := asm.GhubUser{}
if err = json.Unmarshal(rsp.GetUser(), &ghubUser); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
account := usm.User{}
account.Email = ghubUser.Email
account.Name = ghubUser.Name
account.AvatarURL = ghubUser.AvatarURL
account.Login = ghubUser.Login
// Try to get the user in the database
userMsg, err := a.UserSvc.GetUser(ctx, &userpb.UserRequest{Categ: "login", Value: ghubUser.Login})
// If the user was not found
if err != nil && strings.Contains(err.Error(), "not found") {
var data []byte
data, err = account.ToByte()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
userMsg, err = a.UserSvc.CreateUser(ctx, &userpb.UserMessage{User: data})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["auth"] = ghubUser.Login
session.Save(r, w)
w.Write([]byte(userMsg.GetUser()))
}