MCPcopy Index your code
hub / github.com/Preloading/TwitterAPIBridge / access_token

Function access_token

twitterv1/auth.go:24–116  ·  view source on GitHub ↗

https://developer.x.com/en/docs/authentication/api-reference/access_token and https://web.archive.org/web/20120708225149/https://dev.twitter.com/docs/oauth/xauth

(c *fiber.Ctx)

Source from the content-addressed store, hash-verified

22// and
23// https://web.archive.org/web/20120708225149/https://dev.twitter.com/docs/oauth/xauth
24func access_token(c *fiber.Ctx) error {
25 // Parse the form data
26 //sendErrorCodes := c.FormValue("send_error_codes")
27 authMode := c.FormValue("x_auth_mode")
28 authPassword := c.FormValue("x_auth_password")
29 authUsername := c.FormValue("x_auth_username")
30
31 if authMode == "client_auth" {
32 res, pds, err := blueskyapi.Authenticate(authUsername, authPassword)
33 if err != nil {
34 return HandleBlueskyError(c, err.Error(), "com.atproto.server.createSession", access_token)
35 }
36
37 // Our bluesky authentication was sucessful! Now we should store the auth info, encryted, in the DB
38 encryptionkey, err := cryption.GenerateKey()
39 if err != nil {
40 fmt.Println("Error:", err)
41 return ReturnError(c, "Failed to generate encryption key", 131, fiber.StatusInternalServerError)
42 }
43
44 access_token_expiry, err := cryption.GetJWTTokenExpirationUnix(res.AccessJwt)
45 if err != nil {
46 fmt.Println("Error:", err)
47 return ReturnError(c, "Failed to get token expiration.", 131, fiber.StatusInternalServerError)
48 }
49 refresh_token_expiry, err := cryption.GetJWTTokenExpirationUnix(res.RefreshJwt)
50 if err != nil {
51 fmt.Println("Error:", err)
52 return ReturnError(c, "Failed to get token expiration.", 131, fiber.StatusInternalServerError)
53 }
54
55 uuid, err := db_controller.StoreToken(res.DID, *pds, res.AccessJwt, res.RefreshJwt, encryptionkey, *access_token_expiry, *refresh_token_expiry)
56
57 if err != nil {
58 fmt.Println("Error:", err)
59 return ReturnError(c, "Failed to store token, if this persists contact instance operator.", 131, fiber.StatusInternalServerError)
60 }
61 encryptionkey = strings.ReplaceAll(encryptionkey, "+", "-")
62 encryptionkey = strings.ReplaceAll(encryptionkey, "/", "_")
63 encryptionkey = strings.ReplaceAll(encryptionkey, "=", "") // remove padding
64
65 token := jwt.NewWithClaims(jwt.SigningMethodHS256, bridge.AuthToken{
66 Version: 2,
67 Platform: "bluesky",
68 DID: res.DID,
69 CryptoKey: encryptionkey,
70 TokenUUID: *uuid,
71 ServerIdentifier: configData.ServerIdentifier,
72 ServerURLs: configData.ServerURLs,
73 RegisteredClaims: &jwt.RegisteredClaims{
74 // No ExpiresAt field means the token never expires
75 IssuedAt: jwt.NewNumericDate(time.Now()),
76 NotBefore: jwt.NewNumericDate(time.Now()),
77 Issuer: configData.ServerIdentifier,
78 ExpiresAt: jwt.NewNumericDate(time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)), // it dies without this. i guess it's also nice to have ig
79 },
80 })
81

Callers

nothing calls this directly

Calls 8

GenerateKeyFunction · 0.92
StoreTokenFunction · 0.92
StoreAnalyticDataFunction · 0.92
BlueSkyToTwitterIDFunction · 0.92
HandleBlueskyErrorFunction · 0.85
ReturnErrorFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected