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