Skip to content

fix: Resolve infinite recursion in Auth API #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (c *OAuthClient) getAccessToken(ctx context.Context, params getAccessTokenP
if params.Secret != "" {
opt = append(opt, withHTTPHeader(authorizeHeader, fmt.Sprintf("Bearer %s", params.Secret)))
}
if err := c.core.Request(ctx, http.MethodPost, getTokenPath, req, result, opt...); err != nil {
if err := c.core.Request(genAuthContext(ctx), http.MethodPost, getTokenPath, req, result, opt...); err != nil {
return nil, err
}
return result, nil
Expand Down Expand Up @@ -462,7 +462,7 @@ func (c *DeviceOAuthClient) doGetDeviceCode(ctx context.Context, workspaceID *st
ClientID: c.clientID,
}
result := &GetDeviceAuthResp{}
err := c.core.Request(ctx, http.MethodPost, urlPath, req, result)
err := c.core.Request(genAuthContext(ctx), http.MethodPost, urlPath, req, result)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -516,7 +516,7 @@ func (c *DeviceOAuthClient) GetAccessToken(ctx context.Context, dReq *GetDeviceO

func (c *DeviceOAuthClient) doGetAccessToken(ctx context.Context, req *getAccessTokenReq) (*OAuthToken, error) {
resp := &getOAuthTokenResp{}
if err := c.core.Request(ctx, http.MethodPost, getTokenPath, req, resp); err != nil {
if err := c.core.Request(genAuthContext(ctx), http.MethodPost, getTokenPath, req, resp); err != nil {
return nil, err
}
res := &OAuthToken{
Expand Down
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type authTransport struct {
}

func (h *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if isAuthContext(req.Context()) {
return h.next.RoundTrip(req)
}
accessToken, err := h.auth.Token(req.Context())
if err != nil {
logger.Errorf(req.Context(), "Failed to get access token: %v", err)
Expand Down
24 changes: 24 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coze

import (
"context"
"crypto/rand"
"encoding/json"
)
Expand Down Expand Up @@ -48,3 +49,26 @@ func mustToJson(obj any) string {
}
return string(jsonArray)
}

type contextKey string

const (
authContextKey = contextKey("auth_context")
authContextValue = "1"
)

func genAuthContext(ctx context.Context) context.Context {
return context.WithValue(ctx, authContextKey, authContextValue)
}

func isAuthContext(ctx context.Context) bool {
v := ctx.Value(authContextKey)
if v == nil {
return false
}
strV, ok := v.(string)
if !ok {
return false
}
return strV == authContextValue
}
Loading