/
auth.go
175 lines (149 loc) · 5.12 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package api
import (
"context"
"io"
"net/http"
"strconv"
"time"
"github.com/go-chi/jwtauth/v5"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"github.com/scratchdata/scratchdata/pkg/storage/database/models"
"github.com/tidwall/gjson"
)
func UserFromContext(c context.Context) (*models.User, bool) {
userAny := c.Value("user")
user, ok := userAny.(*models.User)
return user, ok
}
func (a *ScratchDataAPIStruct) AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiKey := r.URL.Query().Get("api_key")
hashedKey := a.storageServices.Database.Hash(apiKey)
// If we have an admin api key, then get the database_id from a query param
isAdmin := a.storageServices.Database.VerifyAdminAPIKey(r.Context(), hashedKey)
if isAdmin {
databaseId := r.URL.Query().Get("destination_id")
dbInt, err := strconv.ParseInt(databaseId, 10, 64)
if err != nil {
dbInt = int64(-1)
}
ctx := context.WithValue(r.Context(), "databaseId", dbInt)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
// Otherwise, this API key is specific to a user
keyDetails, err := a.storageServices.Database.GetAPIKeyDetails(r.Context(), hashedKey)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
ctx := context.WithValue(r.Context(), "databaseId", keyDetails.DestinationID)
ctx = context.WithValue(ctx, "teamId", keyDetails.Destination.TeamID)
next.ServeHTTP(w, r.WithContext(ctx))
}
})
}
func (a *ScratchDataAPIStruct) AuthGetDatabaseID(ctx context.Context) int64 {
dbId := ctx.Value("databaseId").(uint)
return int64(dbId)
}
func (a *ScratchDataAPIStruct) AuthGetTeamID(ctx context.Context) uint {
dbId := ctx.Value("teamId").(uint)
return dbId
}
func (a *ScratchDataAPIStruct) Login(w http.ResponseWriter, r *http.Request) {
url := a.googleOauthConfig.AuthCodeURL(uuid.New().String())
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func (a *ScratchDataAPIStruct) Authenticator(ja *jwtauth.JWTAuth) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
hfn := func(w http.ResponseWriter, r *http.Request) {
token, claims, err := jwtauth.FromContext(r.Context())
if token == nil || err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
userId, ok := claims["user_id"]
if !ok {
log.Error().Msg("User ID not found in claims")
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
// w.WriteHeader(http.StatusUnauthorized)
// w.Write([]byte("Unauthorized"))
return
}
user := a.storageServices.Database.GetUser(uint(userId.(float64)))
if user.ID <= 0 {
log.Error().Msg("User not found")
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
// w.WriteHeader(http.StatusUnauthorized)
// w.Write([]byte("Unauthorized"))
return
}
ctx := context.WithValue(r.Context(), "user", user)
// a.Authenticator(a.tokenAuth)(next).ServeHTTP(w, r.WithContext(ctx))
// ctx :=
// next.ServeHTTP(w, r)
// if err != nil {
// http.Error(w, err.Error(), http.StatusUnauthorized)
// return
// }
// if token == nil || jwt.Validate(token, ja.validateOptions...) != nil {
// http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
// return
// }
// Token is authenticated, pass it through
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(hfn)
}
}
func (a *ScratchDataAPIStruct) Logout(w http.ResponseWriter, r *http.Request) {
for _, cookie := range r.Cookies() {
cookie.MaxAge = 0
cookie.Value = ""
http.SetCookie(w, cookie)
}
http.Redirect(w, r, "login", http.StatusSeeOther)
}
func (a *ScratchDataAPIStruct) OAuthCallback(w http.ResponseWriter, r *http.Request) {
// state := r.FormValue("state")
code := r.FormValue("code")
log.Print(code)
token, err := a.googleOauthConfig.Exchange(r.Context(), code)
if err != nil {
log.Error().Err(err).Msg("Unable to exchange code for token")
return
}
resp, err := a.googleOauthConfig.Client(r.Context(), token).Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
log.Error().Err(err).Msg("Unable to get user info")
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("Unable to read response body")
return
}
email := gjson.GetBytes(data, "email").String()
user, err := a.storageServices.Database.CreateUser(email, "google", string(data))
if err != nil {
log.Error().Err(err).Msg("Unable to create user")
return
}
claims := map[string]any{}
claims["user_id"] = user.ID
jwtauth.SetExpiryIn(claims, 7*24*time.Hour)
_, tokenString, err := a.tokenAuth.Encode(claims)
if err != nil {
log.Error().Err(err).Msg("Unable to encode token")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
jwtCookie := &http.Cookie{Name: "jwt", Value: tokenString, HttpOnly: true, Path: "/"}
// jwtCookie := &http.Cookie{Name: "jwt", Value: tokenString, HttpOnly: false}
http.SetCookie(w, jwtCookie)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
}