/
auth.go
135 lines (105 loc) · 3.32 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
package graph
import (
"encoding/json"
"errors"
"net/http"
"os"
"time"
"firebase.google.com/go/auth"
"github.com/golang-jwt/jwt/v4"
e "github.com/pkg/errors"
"github.com/sendgrid/sendgrid-go/helpers/mail"
log "github.com/sirupsen/logrus"
"github.com/highlight-run/highlight/backend/model"
)
type LoginCredentials struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}
var AdminPassword = os.Getenv("ADMIN_PASSWORD")
var AdminPasswordTokenDuration = time.Hour * 24
func (r *Resolver) Login(w http.ResponseWriter, req *http.Request) {
var credentials LoginCredentials
ctx := req.Context()
if AdminPassword == "" {
http.Error(w, "", http.StatusInternalServerError)
log.WithContext(ctx).Error(errors.New("Password auth mode not properly configured."))
return
}
err := json.NewDecoder(req.Body).Decode(&credentials)
if err != nil || credentials.Password == "" || credentials.Email == "" {
log.WithContext(ctx).Error(err)
http.Error(w, "no email or password provided", http.StatusBadRequest)
return
}
_, err = mail.ParseEmail(credentials.Email)
if err != nil {
log.WithContext(ctx).Error(err)
http.Error(w, "Invalid email provided", http.StatusBadRequest)
return
}
if AdminPassword != credentials.Password {
http.Error(w, "invalid password", http.StatusBadRequest)
return
}
user := GetPasswordAuthUser(credentials.Email)
atClaims := jwt.MapClaims{}
atClaims["authorized"] = true
atClaims["exp"] = time.Now().Add(AdminPasswordTokenDuration).Unix()
atClaims["email"] = user.Email
atClaims["uid"] = user.Email
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
token, err := at.SignedString([]byte(JwtAccessSecret))
if err != nil {
log.WithContext(ctx).Error(err)
http.Error(w, "", http.StatusInternalServerError)
}
response := make(map[string]interface{})
response["token"] = token
response["user"] = user
jsonResponse, err := json.Marshal((response))
if err != nil {
log.WithContext(ctx).Error(err)
http.Error(w, "", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(jsonResponse)
if err != nil {
log.WithContext(ctx).Error(e.Wrap(err, "error writing password-auth login response"))
}
}
func (r *Resolver) ValidateAuthToken(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
email := ctx.Value(model.ContextKeys.Email)
uid := ctx.Value(model.ContextKeys.UID)
if email == nil || uid == nil {
log.WithContext(ctx).Error(errors.New("Forbidden"))
http.Error(w, "", http.StatusUnauthorized)
return
}
user := GetPasswordAuthUser(email.(string))
response := make(map[string]interface{})
response["user"] = user
jsonResponse, err := json.Marshal((response))
if err != nil {
log.WithContext(ctx).Error(err)
http.Error(w, "", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(jsonResponse)
if err != nil {
log.WithContext(ctx).Error(e.Wrap(err, "error writing validate-auth-token response"))
}
}
func GetPasswordAuthUser(email string) *auth.UserInfo {
return &auth.UserInfo{
DisplayName: email,
UID: email,
Email: email,
PhoneNumber: "+14081234567",
PhotoURL: "https://picsum.photos/200",
ProviderID: "",
}
}