-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
146 lines (114 loc) · 3.28 KB
/
token.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
auth "github.com/jaydee029/Verses/internal/auth"
"github.com/jaydee029/Verses/internal/database"
)
type Token struct {
Token string `json:"token"`
}
func (cfg *apiconfig) revokeToken(w http.ResponseWriter, r *http.Request) {
token, err := auth.BearerHeader(r.Header)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "bytes couldn't be converted")
return
}
var pgtime pgtype.Timestamp
err = pgtime.Scan(time.Now().UTC())
if err != nil {
fmt.Println("Error setting timestamp:", err)
}
err = cfg.DB.RevokeToken(r.Context(), database.RevokeTokenParams{
Token: []byte(token),
RevokedAt: pgtime,
})
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
}
respondWithJson(w, http.StatusOK, "Token Revoked")
}
func (cfg *apiconfig) verifyRefresh(w http.ResponseWriter, r *http.Request) {
token, err := auth.BearerHeader(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
is_refresh, err := auth.VerifyRefresh(token, cfg.jwtsecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
if !is_refresh {
respondWithError(w, http.StatusUnauthorized, "Header doesn't contain refresh token")
return
}
is_revoked, err := cfg.DB.VerifyRefresh(r.Context(), []byte(token))
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
if is_revoked {
respondWithError(w, http.StatusUnauthorized, "Refresh Token revoked")
return
}
Idstr, err := auth.ValidateToken(token, cfg.jwtsecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
Id, err := uuid.Parse(Idstr)
if err != nil {
fmt.Println("Error parsing string to UUID:", err)
}
auth_token, err := auth.Tokenize(Id, cfg.jwtsecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
respondWithJson(w, http.StatusOK, Token{
Token: auth_token,
})
}
func (cfg *apiconfig) is_gold(w http.ResponseWriter, r *http.Request) {
type user_struct struct {
User_id pgtype.UUID `json:"user_id"`
}
type body struct {
Event string `json:"event"`
Data user_struct `json:"data"`
}
key, err := auth.VerifyAPIkey(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}
if key != cfg.apiKey {
respondWithError(w, http.StatusUnauthorized, "Incorrect API Key")
}
decoder := json.NewDecoder(r.Body)
params := body{}
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't decode parameters")
return
}
if params.Event == "user.upgraded" {
user_res, err := cfg.DB.Is_red(r.Context(), true)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusOK, User{
Name: user_res.Name,
Email: user_res.Email,
Is_red: user_res.IsRed,
ID: params.Data.User_id,
})
}
respondWithJson(w, http.StatusOK, "http request accepted in the webhook")
}