-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
280 lines (244 loc) · 8.42 KB
/
oauth.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// struct used to get the user from paleoid
type Payload struct {
GrantType string `json:"grant_type"` //will always be "authorization_code"
Code string `json:"code"` //the code returned by the oauth server
RedirectUri string `json:"redirect_uri"` //the redirect uri (saved in env variable)
ClientID string `json:"client_id"` //the client id (saved in env variable)
ClientSecret string `json:"client_secret"` //the client secret (saved in env variable)
}
type State struct {
Id primitive.ObjectID `bson:"_id"`
State string `json:"state"`
Issued time.Time `bson:"issDate"`
ExpirationDate time.Time `bson:"expDate"`
RedirectUri string `bson:"redirectUri"`
}
type Polling struct {
DBId primitive.ObjectID `bson:"_id"`
RandomId string `bson:"id"`
IssDate time.Time `bson:"issDate"`
ExpDate time.Time `bson:"expDate"`
LoginSuccessful bool `bson:"loginSuccessful"`
}
// TODO: should separate the generation of the polling id from this function and put it in a separate function
// returns a unique signed base64url encoded state string that lasts 5 minutes (saved on the database)
func CreateState(redirectUri string, saveRedirectUri, savePollingId bool) (string, string, error) {
//connect to the db
db, err := connectToDB()
if err != nil {
return "", "", err
}
defer db.Client().Disconnect(context.TODO())
statesCollection := db.Collection("oauthStates")
//generate a random state string (must not already be on the db)
var state string
for {
state = generateRandomString(24)
var duplicate string
err = statesCollection.FindOne(context.TODO(), bson.M{"state": state}).Decode(&duplicate)
if err != nil {
if err == mongo.ErrNoDocuments {
break
}
}
}
insert := bson.M{"state": state, "issDate": time.Now(), "expDate": time.Now().Add(time.Minute * 5)}
if saveRedirectUri {
insert["redirectUri"] = redirectUri
}
_, err = statesCollection.InsertOne(context.TODO(), insert)
if err != nil {
return "", "", err
}
var pollingID string
if savePollingId {
pollingCollection := db.Collection("pollingIDs")
for {
pollingID = generateRandomString(24)
var duplicate string
err = pollingCollection.FindOne(context.TODO(), bson.M{"id": pollingID}).Decode(&duplicate)
if err != nil {
if err == mongo.ErrNoDocuments {
break
}
}
}
insertPolling := bson.M{"id": pollingID, "state": state, "issDate": time.Now(), "expDate": time.Now().Add(time.Minute * 5), "loginSuccessful": false}
_, err = pollingCollection.InsertOne(context.TODO(), insertPolling)
if err != nil {
return "", "", err
}
}
//encrypt the state
//encryptedBytes, err := rsa.EncryptOAEP(
// sha256.New(),
// rand.Reader,
// publicKey,
// []byte(state),
// nil)
//if err != nil {
// return "", "", err
//}
//encode the encrypted state with base64url
return base64.StdEncoding.EncodeToString([]byte(state)), pollingID, nil
}
// check if the encrypted state is valid and if so returnes true and delete the state from the database
func CheckState(cypher string) (valid bool, redirectUri string, state string, err error) {
//replace the spaces with + signs in the cypher
cypher = strings.Replace(cypher, " ", "+", -1)
//decode the cypher with base64url
decoded, err := base64.StdEncoding.DecodeString(cypher)
if err != nil {
return false, "", "", err
}
//decrypt the cypher with the private key
//decryptedBytes, err := privateKey.Decrypt(nil, decoded, &rsa.OAEPOptions{Hash: crypto.SHA256})
//if err != nil {
// return false, "", "", err
//}
db, err := connectToDB()
if err != nil {
return false, "", "", err
}
defer db.Client().Disconnect(context.TODO())
//check if the state is actually found
state = string(decoded)
fmt.Println(state)
stateCollection := db.Collection("oauthStates")
var s State
err = stateCollection.FindOne(context.TODO(), bson.M{"state": state}).Decode(&s)
fmt.Println(s)
if err != nil {
if err == mongo.ErrNoDocuments {
fmt.Println("ahhhh non ho trovato nulla :(")
return false, "", "", fmt.Errorf("state not found")
}
return false, "", "", err
}
//delete the state from the database and check if it's still valid
//(should delete it even if it's expired, so we delete it before check if it's expired)
_, err = stateCollection.DeleteOne(context.TODO(), bson.M{"state": state})
if err != nil {
return false, "", "", err
}
if time.Since(s.Issued) > time.Minute*5 {
return false, "", "", fmt.Errorf("state expired, it was issued %v ago", time.Since(s.Issued))
}
return true, s.RedirectUri, state, nil
}
func GetPollingIDFromState(state string, connection *mongo.Database) (pollingID string, found bool, err error) {
var polling Polling
pollingCollection := connection.Collection("pollingIDs")
err = pollingCollection.FindOne(context.TODO(), bson.M{"state": state}).Decode(&polling)
if err != nil {
if err == mongo.ErrNoDocuments {
return "", false, nil
}
return "", false, err
}
return polling.RandomId, true, nil
}
func UpdatePollingID(randomID, accessToken, refreshToken string) error {
db, err := connectToDB()
if err != nil {
return err
}
pollingCollection := db.Collection("pollingIDs")
var found Polling
err = pollingCollection.FindOne(context.TODO(), bson.M{"id": randomID}).Decode(&found)
if err != nil {
if err == mongo.ErrNoDocuments {
return fmt.Errorf("%s was not found", randomID)
} else {
return err
}
}
update := bson.D{{"$set", bson.D{{"accessToken", accessToken}, {"refreshToken", refreshToken}, {"loginSuccessful", true}}}}
_, err = pollingCollection.UpdateOne(context.TODO(), bson.M{"id": randomID}, update)
return err
}
// given the code generate from the paleoid server it returns the access token of the student
// this section is documented on the official paleoid documentation of how to retireve the access token
// https://paleoid.stoplight.io/docs/api/b3A6NDE0Njg2Mw-ottieni-un-access-token
func GetPaleoIDAccessToken(code string) (string, error) {
//do post request to url with the code and the env variables
//(they are envs because they are private and saved in the .env)
url := "https://id.paleo.bg.it/oauth/token"
payload := Payload{
GrantType: "authorization_code",
Code: code,
RedirectUri: os.Getenv("REDIRECT_URI"),
ClientID: os.Getenv("OAUTH_ID"),
ClientSecret: os.Getenv("OAUTH_SECRET"),
}
//encode the payload
jsonPayload, err := json.Marshal(payload)
if err != nil {
return "", err
}
//do the push request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
//get the access token
//I decided to use strings replace because it's easier and doesn't require a struct to unmarshal
accessToken := string(body)
accessToken = strings.Replace(accessToken, `{"access_token":"`, "", -1)
//length of the paleoid access token
accessToken = accessToken[:129]
accessToken = strings.Replace(accessToken, "\n", "", -1)
return accessToken, nil
}
// return a student struct given the access token
// this section is documented on the official paleoid documentation of
// how to retireve the student data from the access token
// https://paleoid.stoplight.io/docs/api/b3A6NDIwMTA1Mw-ottieni-le-informazioni-dell-utente
func GetStudentFromPaleoIDAccessToken(accessToken string) (Student, error) {
url := "https://id.paleo.bg.it/api/v2/user"
//make a get request to url with the access token as Bearer token
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Student{}, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
//make the request
res, err := http.DefaultClient.Do(req)
if err != nil {
return Student{}, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return Student{}, err
}
//parse the body into a student struct (from the json response)
var student Student
student.IsMock = false
err = json.Unmarshal(body, &student)
return student, err
}