@@ -38,6 +38,7 @@ type SNCloudContext struct {
3838 IssuerURL string
3939 Audience string
4040 KeyFilePath string
41+ JWTToken string
4142 APIURL string
4243 LogAPIURL string
4344 Timeout time.Duration
@@ -53,10 +54,12 @@ type Session struct {
5354 APIClient * sncloud.APIClient
5455 LogClient * http.Client
5556 TokenRefresher * OAuth2TokenRefresher
57+ TokenSource oauth2.TokenSource
5658 Configuration * sncloud.Configuration
5759 mutex sync.RWMutex
5860 apiClientOnce sync.Once
5961 logClientOnce sync.Once
62+ useJWT bool
6063}
6164
6265// OAuth2TokenRefresher implements oauth2.TokenSource interface for refreshing OAuth2 tokens
@@ -84,15 +87,42 @@ func (t *OAuth2TokenRefresher) Token() (*oauth2.Token, error) {
8487 return t .source .Token ()
8588}
8689
90+ // JWTTokenSource implements oauth2.TokenSource interface for static JWT tokens
91+ type JWTTokenSource struct {
92+ token * oauth2.Token
93+ }
94+
95+ // NewJWTTokenSource creates a new token source for static JWT tokens
96+ func NewJWTTokenSource (jwtToken string ) * JWTTokenSource {
97+ return & JWTTokenSource {
98+ token : & oauth2.Token {
99+ AccessToken : jwtToken ,
100+ TokenType : "Bearer" ,
101+ },
102+ }
103+ }
104+
105+ // Token implements the oauth2.TokenSource interface for static JWT tokens
106+ func (j * JWTTokenSource ) Token () (* oauth2.Token , error ) {
107+ return j .token , nil
108+ }
109+
87110// NewSNCloudSession creates a new StreamNative Cloud session with the provided context
88111func NewSNCloudSession (ctx SNCloudContext ) (* Session , error ) {
89112 session := & Session {
90113 Ctx : ctx ,
91114 }
92115
93- // Initialize the session by setting up the token refresher
94- if err := session .initializeTokenRefresher (); err != nil {
95- return nil , errors .Wrap (err , "failed to initialize token refresher" )
116+ // Check if JWT token is provided
117+ if ctx .JWTToken != "" {
118+ // Use JWT token directly without refresh mechanism
119+ session .useJWT = true
120+ session .TokenSource = NewJWTTokenSource (ctx .JWTToken )
121+ } else {
122+ // Initialize the session by setting up the token refresher for OAuth flow
123+ if err := session .initializeTokenRefresher (); err != nil {
124+ return nil , errors .Wrap (err , "failed to initialize token refresher" )
125+ }
96126 }
97127
98128 return session , nil
@@ -198,13 +228,19 @@ func (s *Session) GetAPIClient() (*sncloud.APIClient, error) {
198228
199229// initializeAPIClient initializes the API client for the session
200230func (s * Session ) initializeAPIClient () error {
201- if s .TokenRefresher == nil {
202- return errors .New ("token refresher not initialized" )
231+ var tokenSource oauth2.TokenSource
232+
233+ if s .useJWT {
234+ // Use JWT token directly
235+ tokenSource = s .TokenSource
236+ } else {
237+ // Use OAuth token with refresh
238+ if s .TokenRefresher == nil {
239+ return errors .New ("token refresher not initialized" )
240+ }
241+ tokenSource = oauth2 .ReuseTokenSource (nil , s .TokenRefresher )
203242 }
204243
205- // Create token source with reuse
206- tokenSource := oauth2 .ReuseTokenSource (nil , s .TokenRefresher )
207-
208244 // Create HTTP client with OAuth2 Transport
209245 httpClient := & http.Client {
210246 Transport : & oauth2.Transport {
@@ -249,13 +285,19 @@ func (s *Session) GetLogClient() (*http.Client, error) {
249285
250286// initializeLogClient initializes the log client for the session
251287func (s * Session ) initializeLogClient () error {
252- if s .TokenRefresher == nil {
253- return errors .New ("token refresher not initialized" )
288+ var tokenSource oauth2.TokenSource
289+
290+ if s .useJWT {
291+ // Use JWT token directly
292+ tokenSource = s .TokenSource
293+ } else {
294+ // Use OAuth token with refresh
295+ if s .TokenRefresher == nil {
296+ return errors .New ("token refresher not initialized" )
297+ }
298+ tokenSource = oauth2 .ReuseTokenSource (nil , s .TokenRefresher )
254299 }
255300
256- // Create token source with reuse
257- tokenSource := oauth2 .ReuseTokenSource (nil , s .TokenRefresher )
258-
259301 // Create HTTP client with OAuth2 Transport
260302 s .LogClient = & http.Client {
261303 Timeout : 10 * time .Second ,
0 commit comments