Skip to content

Commit ae59b50

Browse files
committed
fix: null did crashing program
1 parent 3b9b1a8 commit ae59b50

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
_ "net/http/pprof"
56

67
"github.com/Preloading/TwitterAPIBridge/config"
78
"github.com/Preloading/TwitterAPIBridge/db_controller"
@@ -13,6 +14,11 @@ var (
1314
)
1415

1516
func main() {
17+
// Enable pprof for debugging purposes
18+
// go func() {
19+
// log.Println(http.ListenAndServe("localhost:6060", nil))
20+
// }()
21+
1622
var err error
1723
configData, err = config.LoadConfig()
1824
if err != nil {

twitterv1/auth.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
148148
var err error
149149

150150
isBasic := false
151+
nodid := ""
151152
var username string
152153

153154
// Define a regular expression to match the oauth_token
@@ -160,7 +161,7 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
160161
var did *string
161162
basicAuthUsernamePassword, err = cryption.Base64URLDecode(base64pass)
162163
if err != nil {
163-
return nil, &fallbackRoute, nil, nil, err
164+
return &nodid, &fallbackRoute, nil, nil, err
164165
}
165166

166167
// separate the username and password
@@ -174,32 +175,32 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
174175
if err.Error() == "invalid credentials" {
175176
// test if password is an app password thru regex
176177
if !regexp.MustCompile(`^[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}$`).MatchString(authPassword) {
177-
return nil, &fallbackRoute, nil, nil, errors.New("invalid app password")
178+
return &nodid, &fallbackRoute, nil, nil, errors.New("invalid app password")
178179
}
179180

180181
res, pds, err := blueskyapi.Authenticate(username, authPassword)
181182
if err != nil {
182-
return nil, &fallbackRoute, nil, nil, err
183+
return &nodid, &fallbackRoute, nil, nil, err
183184
}
184185

185186
access_token_expiry, err := cryption.GetJWTTokenExpirationUnix(res.AccessJwt)
186187
if err != nil {
187-
return nil, &fallbackRoute, nil, nil, errors.New("failed to get access token expiry")
188+
return &nodid, &fallbackRoute, nil, nil, errors.New("failed to get access token expiry")
188189
}
189190
refresh_token_expiry, err := cryption.GetJWTTokenExpirationUnix(res.RefreshJwt)
190191
if err != nil {
191-
return nil, &fallbackRoute, nil, nil, errors.New("failed to get refresh token expiry")
192+
return &nodid, &fallbackRoute, nil, nil, errors.New("failed to get refresh token expiry")
192193
}
193194

194195
_, err = db_controller.StoreTokenBasic(res.DID, *pds, res.AccessJwt, res.RefreshJwt, username, authPassword, *access_token_expiry, *refresh_token_expiry)
195196

196197
if err != nil {
197-
return nil, &fallbackRoute, nil, nil, err
198+
return &nodid, &fallbackRoute, nil, nil, err
198199
}
199200

200201
return &res.DID, pds, nil, &res.AccessJwt, nil // TODO: Maybe change the uuid to something for here?
201202
} else {
202-
return nil, &fallbackRoute, nil, nil, err
203+
return &nodid, &fallbackRoute, nil, nil, err
203204
}
204205
}
205206

@@ -208,7 +209,7 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
208209
re := regexp.MustCompile(`oauth_token="([^"]+)"`)
209210
matches := re.FindStringSubmatch(authHeader)
210211
if len(matches) < 2 {
211-
return nil, &fallbackRoute, nil, nil, errors.New("oauth token not found")
212+
return &nodid, &fallbackRoute, nil, nil, errors.New("oauth token not found")
212213
}
213214

214215
oauthToken := matches[1]
@@ -220,7 +221,7 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
220221
tokenData, err = ConvertV1TokenToV2(oauthToken)
221222

222223
if err != nil {
223-
return nil, &fallbackRoute, nil, nil, err
224+
return &nodid, &fallbackRoute, nil, nil, err
224225
}
225226

226227
} else {
@@ -229,18 +230,18 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
229230
})
230231

231232
if err != nil {
232-
return nil, &fallbackRoute, nil, nil, errors.New("invalid token")
233+
return &nodid, &fallbackRoute, nil, nil, errors.New("invalid token")
233234
}
234235

235236
if !token.Valid {
236237
if tokenData.ServerIdentifier == "" {
237-
return nil, &fallbackRoute, nil, nil, errors.New("invalid token")
238+
return &nodid, &fallbackRoute, nil, nil, errors.New("invalid token")
238239
} else if tokenData.ServerIdentifier != configData.ServerIdentifier {
239-
return nil, &fallbackRoute, nil, nil, errors.New("incorrect server")
240+
return &nodid, &fallbackRoute, nil, nil, errors.New("incorrect server")
240241

241242
}
242243

243-
return nil, &fallbackRoute, nil, nil, errors.New("invalid token")
244+
return &nodid, &fallbackRoute, nil, nil, errors.New("invalid token")
244245
}
245246
}
246247

@@ -257,7 +258,7 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
257258
accessJwt, refreshJwt, access_expiry, refresh_expiry, userPDS, err = db_controller.GetToken(string(userDID), string(tokenUUID), encryptionKey, tokenType) // Use token version 2 for OAuth
258259

259260
if err != nil {
260-
return nil, &fallbackRoute, nil, nil, err
261+
return &nodid, &fallbackRoute, nil, nil, err
261262
}
262263
}
263264

@@ -293,25 +294,25 @@ func GetAuthFromReq(c *fiber.Ctx) (*string, *string, *string, *string, error) {
293294
} else {
294295
db_controller.DeleteToken(string(userDID), string(tokenUUID))
295296
}
296-
return nil, &fallbackRoute, nil, nil, errors.New("refresh token has expired")
297+
return &nodid, &fallbackRoute, nil, nil, errors.New("refresh token has expired")
297298
}
298299

299300
// Our refresh token is still valid. Lets refresh our access token.
300301
new_auth, err := blueskyapi.RefreshToken(*userPDS, *refreshJwt)
301302

302303
if err != nil {
303-
return nil, &fallbackRoute, nil, nil, err
304+
return &nodid, &fallbackRoute, nil, nil, err
304305
}
305306

306307
accessJwt = &new_auth.AccessJwt
307308

308309
access_token_expiry, err := cryption.GetJWTTokenExpirationUnix(new_auth.AccessJwt)
309310
if err != nil {
310-
return nil, &fallbackRoute, nil, nil, errors.New("failed to get access token expiry")
311+
return &nodid, &fallbackRoute, nil, nil, errors.New("failed to get access token expiry")
311312
}
312313
refresh_token_expiry, err := cryption.GetJWTTokenExpirationUnix(new_auth.RefreshJwt)
313314
if err != nil {
314-
return nil, &fallbackRoute, nil, nil, errors.New("failed to get refresh token expiry")
315+
return &nodid, &fallbackRoute, nil, nil, errors.New("failed to get refresh token expiry")
315316
}
316317

317318
// TODO: Recheck if the user id is still bound to that PDS

0 commit comments

Comments
 (0)