this repo has no description
1package main 2 3import ( 4 "context" 5 "fmt" 6 "time" 7 8 oauth "github.com/haileyok/atproto-oauth-golang" 9 "github.com/labstack/echo-contrib/session" 10 "github.com/labstack/echo/v4" 11) 12 13func (s *TestServer) getOauthSession(ctx context.Context, did string) (*OauthSession, error) { 14 var oauthSession OauthSession 15 if err := s.db.Raw("SELECT * FROM oauth_sessions WHERE did = ?", did).Scan(&oauthSession).Error; err != nil { 16 return nil, err 17 } 18 19 if oauthSession.Did == "" { 20 return nil, fmt.Errorf("did not find session in database") 21 } 22 23 if oauthSession.Expiration.Sub(time.Now()) <= 5*time.Minute { 24 privateJwk, err := oauth.ParseJWKFromBytes([]byte(oauthSession.DpopPrivateJwk)) 25 if err != nil { 26 return nil, err 27 } 28 29 resp, err := s.oauthClient.RefreshTokenRequest(ctx, oauthSession.RefreshToken, oauthSession.AuthserverIss, oauthSession.DpopAuthserverNonce, privateJwk) 30 if err != nil { 31 return nil, err 32 } 33 34 expiration := time.Now().Add(time.Duration(int(time.Second) * int(resp.ExpiresIn))) 35 36 if err := s.db.Exec("UPDATE oauth_sessions SET access_token = ?, refresh_token = ?, dpop_authserver_nonce = ?, expiration = ? WHERE did = ?", resp.AccessToken, resp.RefreshToken, resp.DpopAuthserverNonce, expiration, oauthSession.Did).Error; err != nil { 37 return nil, err 38 } 39 40 oauthSession.AccessToken = resp.AccessToken 41 oauthSession.RefreshToken = resp.RefreshToken 42 oauthSession.DpopAuthserverNonce = resp.DpopAuthserverNonce 43 oauthSession.Expiration = expiration 44 } 45 46 return &oauthSession, nil 47} 48 49func (s *TestServer) getOauthSessionAuthArgs(e echo.Context) (*oauth.XrpcAuthedRequestArgs, bool, error) { 50 sess, err := session.Get("session", e) 51 if err != nil { 52 return nil, false, err 53 } 54 55 did, ok := sess.Values["did"].(string) 56 if !ok { 57 return nil, false, nil 58 } 59 60 oauthSession, err := s.getOauthSession(e.Request().Context(), did) 61 62 privateJwk, err := oauth.ParseJWKFromBytes([]byte(oauthSession.DpopPrivateJwk)) 63 if err != nil { 64 return nil, false, err 65 } 66 67 return &oauth.XrpcAuthedRequestArgs{ 68 Did: oauthSession.Did, 69 AccessToken: oauthSession.AccessToken, 70 PdsUrl: oauthSession.PdsUrl, 71 Issuer: oauthSession.AuthserverIss, 72 DpopPdsNonce: oauthSession.DpopPdsNonce, 73 DpopPrivateJwk: privateJwk, 74 }, true, nil 75}