mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:36:49 +00:00
Drop the last old-stack coupling: ai no longer links the retired Casdoor SDK
module github.com/hanzoai/iam-v1 (which dragged beego/xorm/ldap/aliyun/aws into
ai's build for server code ai never ran). Replace it with an ai-internal
package internal/iam (package iam) that talks to Hanzo IAM (hanzo.id) over
standard OIDC — JWKS token verification against {endpoint}/v1/iam/.well-known/
jwks — and the IAM server's JSON REST API (/v1/iam/get-user, get-organization,
get-application, permissions, resources, send-email, oauth token exchange).
- internal/iam mirrors the IAM wire models. User/Permission/Resource keep the
FULL field set with json tags: object/redact.go reflects over every User
field by json tag as a fail-secure secret-redaction control, so a slim User
would silently weaken it and break redact_test. Read-only response types
(Organization/Application/Provider/Cert) are projections of the fields ai reads.
- All 79 importing files swap github.com/hanzoai/iam-v1 →
github.com/hanzoai/ai/internal/iam (package name unchanged, so call sites are
untouched). iam-v1 require dropped; go mod tidy.
- No Casdoor call was stubbed: SendEmail/UploadResource/GetResources/
DeleteResourceWithTag were already clean /v1/iam/ REST calls, reproduced
faithfully (zero behavior change), not faked.
Verify: CGO_ENABLED=0 go build ./... clean; go vet ./... clean; redaction
security tests pass (TestRedactUserSecrets/AllowlistFailSecure/Claims/NilSafe).
Co-authored-by: Hanzo Dev <[email protected]>
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package iam
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// OAuthOption configures an OAuth request.
|
|
type OAuthOption func(*oauthOptions)
|
|
|
|
type oauthOptions struct {
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// WithHTTPClient sets a custom http client for the OAuth exchange.
|
|
func WithHTTPClient(httpClient *http.Client) OAuthOption {
|
|
return func(opts *oauthOptions) { opts.httpClient = httpClient }
|
|
}
|
|
|
|
// GetOAuthToken exchanges an authorization code for a token against the IAM
|
|
// server's OAuth endpoints.
|
|
func (c *Client) GetOAuthToken(code, state string, opts ...OAuthOption) (*oauth2.Token, error) {
|
|
options := &oauthOptions{}
|
|
for _, opt := range opts {
|
|
opt(options)
|
|
}
|
|
|
|
endpoint := c.endpoint()
|
|
config := oauth2.Config{
|
|
ClientID: c.ClientId,
|
|
ClientSecret: c.ClientSecret,
|
|
Endpoint: oauth2.Endpoint{
|
|
AuthURL: fmt.Sprintf("%s/v1/iam/oauth/authorize", endpoint),
|
|
TokenURL: fmt.Sprintf("%s/v1/iam/oauth/access_token", endpoint),
|
|
AuthStyle: oauth2.AuthStyleInParams,
|
|
},
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if options.httpClient != nil {
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, options.httpClient)
|
|
}
|
|
|
|
token, err := config.Exchange(ctx, code)
|
|
if err != nil {
|
|
return token, err
|
|
}
|
|
if strings.HasPrefix(token.AccessToken, "error:") {
|
|
return nil, errors.New(strings.TrimPrefix(token.AccessToken, "error: "))
|
|
}
|
|
return token, nil
|
|
}
|
|
|
|
// GetOAuthToken exchanges a code using the configured (or env-derived) client.
|
|
func GetOAuthToken(code, state string, opts ...OAuthOption) (*oauth2.Token, error) {
|
|
return ensureClient().GetOAuthToken(code, state, opts...)
|
|
}
|