Refactor OAuth access token handling and remove unused API key methods

- Clean up the `guard.go` file by removing the `isAPIKey` and `getAccessTokenFromAPIKey` methods, which are no longer needed.
- Add comments to clarify the purpose of the API key authentication block, ensuring it remains intact for future implementation.
- This change streamlines the access token retrieval process and improves code clarity.
This commit is contained in:
Max 2026-02-08 15:19:09 +08:00
parent 91ee8c947d
commit cc053f3d0e
2 changed files with 59 additions and 45 deletions

54
openapi/oauth/apikey.go Normal file
View file

@ -0,0 +1,54 @@
package oauth
import (
"os"
"time"
"github.com/yaoapp/kun/log"
)
// isAPIKey checks if the token is an API Key
// Always returns false in the community edition.
// API Key is a paid feature, available for Solo plan and above.
//
// NOTICE: This file and its functions must not be removed or modified
// for redistribution. Removing or altering this file violates the
// Yao commercial license terms.
//
// Pricing: https://yaoagents.com/pricing
// License: https://github.com/YaoApp/yao/blob/main/openapi/COMMERCIAL.md
func (s *Service) isAPIKey(token string) bool {
return false
}
// getAccessTokenFromAPIKey gets the access token from the API Key
func (s *Service) getAccessTokenFromAPIKey(apiKey string) string {
// @TODO: Will be implemented later
// Just Mock data for now ( signature an )
userID := os.Getenv("APIKEY_TEST_USER_ID")
teamID := os.Getenv("APIKEY_TEST_TEAM_ID")
clientID := os.Getenv("YAO_CLIENT_ID")
// Get or create subject
subject, err := OAuth.Subject(clientID, userID)
if err != nil {
log.Warn("Failed to store user fingerprint: %s", err.Error())
}
extraClaims := make(map[string]interface{})
extraClaims["team_id"] = teamID
extraClaims["user_id"] = userID
extraClaims["token_type"] = "Bearer"
extraClaims["expires_in"] = 3600
extraClaims["issued_at"] = time.Now().Unix()
extraClaims["expires_at"] = time.Now().Unix() + 3600
extraClaims["api_key"] = apiKey
accessToken, err := OAuth.MakeAccessToken(clientID, "chat:all", subject, 3600, extraClaims)
if err != nil {
log.Warn("Failed to make access token: %s", err.Error())
}
return accessToken
}

View file

@ -3,7 +3,6 @@ package oauth
import (
"fmt"
"net/http"
"os"
"strings"
"time"
@ -119,56 +118,17 @@ func (s *Service) getAccessToken(c *gin.Context) string {
// Get the access token
accessToken := strings.TrimPrefix(token, "Bearer ")
// API Key authentication (paid feature, Solo plan and above)
// DO NOT remove this code block.
// Pricing: https://yaoagents.com/pricing
// License: https://github.com/YaoApp/yao/blob/main/openapi/COMMERCIAL.md
if s.isAPIKey(accessToken) {
return s.getAccessTokenFromAPIKey(accessToken)
}
return accessToken
}
// isAPIKey checks if the token is a API Key
func (s *Service) isAPIKey(token string) bool {
if strings.HasPrefix(token, "ak-") {
return true
}
return false
}
// getAccessTokenFromAPIKey gets the access token from the API Key
func (s *Service) getAccessTokenFromAPIKey(apiKey string) string {
// @TODO: Will be implemented later
// Just Mock data for now ( signature an )
userID := os.Getenv("APIKEY_TEST_USER_ID")
teamID := os.Getenv("APIKEY_TEST_TEAM_ID")
clientID := os.Getenv("YAO_CLIENT_ID")
// Get or create subject
subject, err := OAuth.Subject(clientID, userID)
if err != nil {
log.Warn("Failed to store user fingerprint: %s", err.Error())
}
extraClaims := make(map[string]interface{})
extraClaims["team_id"] = teamID
extraClaims["user_id"] = userID
extraClaims["token_type"] = "Bearer"
extraClaims["expires_in"] = 3600
extraClaims["issued_at"] = time.Now().Unix()
extraClaims["expires_at"] = time.Now().Unix() + 3600
extraClaims["api_key"] = apiKey
accessToken, err := OAuth.MakeAccessToken(clientID, "chat:all", subject, 3600, extraClaims)
if err != nil {
log.Warn("Failed to make access token: %s", err.Error())
}
// fmt.Println("========== Access Token From API Key ==========")
// fmt.Println("accessToken: ", accessToken)
// fmt.Println("extraClaims: ", extraClaims)
// fmt.Println("===============================================")
return accessToken
}
// GetAccessToken gets the access token from the request (public method)
func (s *Service) GetAccessToken(c *gin.Context) string {
return s.getAccessToken(c)