Add projects
This commit is contained in:
parent
2d3a9ad623
commit
8b607dd700
1802 changed files with 503346 additions and 2 deletions
76
backend/internal/runtime/executor/xai_executor_auth.go
Normal file
76
backend/internal/runtime/executor/xai_executor_auth.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
xaiauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/xai"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Refresh refreshes xAI OAuth credentials using the stored refresh token.
|
||||
func (e *XAIExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) {
|
||||
log.Debugf("xai executor: refresh called")
|
||||
if refreshed, handled, err := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled {
|
||||
return refreshed, err
|
||||
}
|
||||
if auth == nil {
|
||||
return nil, statusErr{code: http.StatusInternalServerError, msg: "xai executor: auth is nil"}
|
||||
}
|
||||
refreshToken := xaiMetadataString(auth.Metadata, "refresh_token")
|
||||
if refreshToken == "" {
|
||||
return auth, nil
|
||||
}
|
||||
tokenEndpoint := xaiMetadataString(auth.Metadata, "token_endpoint")
|
||||
svc := xaiauth.NewXAIAuthWithProxyURL(e.cfg, auth.ProxyURL)
|
||||
td, err := svc.RefreshTokens(ctx, refreshToken, tokenEndpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if auth.Metadata == nil {
|
||||
auth.Metadata = make(map[string]any)
|
||||
}
|
||||
auth.Metadata["type"] = "xai"
|
||||
auth.Metadata["auth_kind"] = "oauth"
|
||||
auth.Metadata["access_token"] = td.AccessToken
|
||||
if td.RefreshToken != "" {
|
||||
auth.Metadata["refresh_token"] = td.RefreshToken
|
||||
}
|
||||
if td.IDToken != "" {
|
||||
auth.Metadata["id_token"] = td.IDToken
|
||||
}
|
||||
if td.TokenType != "" {
|
||||
auth.Metadata["token_type"] = td.TokenType
|
||||
}
|
||||
if td.ExpiresIn > 0 {
|
||||
auth.Metadata["expires_in"] = td.ExpiresIn
|
||||
}
|
||||
if td.Expire != "" {
|
||||
auth.Metadata["expired"] = td.Expire
|
||||
}
|
||||
if td.Email != "" {
|
||||
auth.Metadata["email"] = td.Email
|
||||
}
|
||||
if td.Subject != "" {
|
||||
auth.Metadata["sub"] = td.Subject
|
||||
}
|
||||
if tokenEndpoint != "" {
|
||||
auth.Metadata["token_endpoint"] = tokenEndpoint
|
||||
}
|
||||
if xaiMetadataString(auth.Metadata, "base_url") == "" {
|
||||
auth.Metadata["base_url"] = xaiauth.DefaultAPIBaseURL
|
||||
}
|
||||
auth.Metadata["last_refresh"] = time.Now().UTC().Format(time.RFC3339)
|
||||
if auth.Attributes == nil {
|
||||
auth.Attributes = make(map[string]string)
|
||||
}
|
||||
auth.Attributes["auth_kind"] = "oauth"
|
||||
if strings.TrimSpace(auth.Attributes["base_url"]) == "" {
|
||||
auth.Attributes["base_url"] = xaiauth.DefaultAPIBaseURL
|
||||
}
|
||||
return auth, nil
|
||||
}
|
||||
Loading…
Reference in a new issue