mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
POST /v1/iam/impersonate answers a single-use login_hint, never a token.
The site starts its own OIDC sign-in with it (third-party-initiated login),
so the code is bound to the site's client, redirect_uri and PKCE, and is
redeemed at the ordinary token endpoint. Authorize honours the hint only in
a browser holding the operator's own session, and only for a client named
in IAM_IMPERSONATION_APPS (unset: none).
The token is the target's (sub, owner, name, orgs) with act {sub, owner,
name} and imp: true, at most fifteen minutes, with no refresh token. The
rule (operator is a SuperAdmin on the row; target a person in a tenant) is
asked again at the hint, the code, the token and every Guard request.
An imp token reads IAM and writes nothing: the Guard refuses its non-GETs,
self-service writes resolve nobody for it, /signin refuses its code, and an
exchange keeps the mark and the clock. act and imp ride UserInfo and
introspection. Every step and refusal is an ActionImpersonate row naming
the operator, filed under the target's org.
LLM.md records the change of stance and the contract cloud and the sites
must ship before the allow-list names them.
Claude-Session: https://claude.ai/code/session_01HvACxex9ZZTzJCCHgirsGy
99 lines
3.7 KiB
Go
99 lines
3.7 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/internal/httpx"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// GET /v1/iam/linked-accounts — the caller's linked social/OAuth identities.
|
|
//
|
|
// schema.User has NO single "linkedIdentities" array; a linked identity is stored as
|
|
// the connector's own column (User.GitHub, User.Google, …) holding the federated
|
|
// subject — the legacy data model. So the linked accounts ARE those per-connector
|
|
// columns that are set; this returns [{provider, subject}] for each non-empty one.
|
|
// Each item carries only the subject string (the schema stores no per-link display
|
|
// name / avatar / linkedAt), so a richer per-link shape is not available from iam.
|
|
// Self-scoped: resolved from the caller (callerOf), never the request.
|
|
|
|
// PathLinkedAccounts is the canonical linked-identities endpoint.
|
|
const PathLinkedAccounts = "/v1/iam/linked-accounts"
|
|
|
|
// connectorTags is the set of User json tags that hold a linked federated-identity
|
|
// subject — the legacy per-connector columns (schema/user.go "Linked
|
|
// federated-identity subjects"). ONE list; linked-accounts reflects the non-empty
|
|
// ones out, so a new connector column is picked up by adding its tag here only.
|
|
var connectorTags = fields(
|
|
"github google qq wechat facebook dingtalk weibo gitee linkedin wecom lark gitlab " +
|
|
"adfs baidu alipay iam infoflow apple azuread azureadb2c slack steam bilibili okta " +
|
|
"douyin kwai line amazon auth0 battlenet bitbucket box cloudfoundry dailymotion deezer " +
|
|
"digitalocean discord dropbox eveonline fitbit gitea heroku influxcloud instagram " +
|
|
"intercom kakao lastfm mailru meetup microsoftonline naver nextcloud onedrive oura " +
|
|
"patreon paypal salesforce shopify soundcloud spotify strava stripe telegram tiktok " +
|
|
"tumblr twitch twitter typetalk uber vk wepay xero yahoo yammer yandex zoom " +
|
|
"custom custom2 custom3 custom4 custom5 custom6 custom7 custom8 custom9 custom10")
|
|
|
|
// linkedAccount is one linked social/OAuth identity.
|
|
type linkedAccount struct {
|
|
Provider string `json:"provider"`
|
|
Subject string `json:"subject"`
|
|
}
|
|
|
|
// linkedAccountsHandler returns the sign-in identities linked to the calling
|
|
// person's account — every provider they can currently sign in with. It is what
|
|
// a security page lists next to the option to disconnect one.
|
|
func linkedAccountsHandler(db orm.DB) zip.Handler {
|
|
return func(c *zip.Ctx) error {
|
|
ctx := c.Context()
|
|
owner, name, ok := readerOf(ctx, c, db)
|
|
if !ok {
|
|
return httpx.Err(c, "please sign in first")
|
|
}
|
|
user, err := store.GetUserByName(ctx, db, owner, name)
|
|
if err != nil {
|
|
return httpx.Err(c, "server_error")
|
|
}
|
|
if user == nil {
|
|
return httpx.Err(c, "the user does not exist")
|
|
}
|
|
return httpx.Ok(c, linkedAccountsOf(user))
|
|
}
|
|
}
|
|
|
|
// linkedAccountsOf reflects a user's non-empty connector columns into the linked list.
|
|
// Reflection over the ONE connectorTags set avoids a per-field cascade and stays
|
|
// faithful to whichever columns hold a subject.
|
|
func linkedAccountsOf(u *schema.User) []linkedAccount {
|
|
out := []linkedAccount{}
|
|
v := reflect.ValueOf(*u)
|
|
t := v.Type()
|
|
for i := 0; i < t.NumField(); i++ {
|
|
tag, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",")
|
|
if !connectorTags[tag] {
|
|
continue
|
|
}
|
|
if s := v.Field(i).String(); s != "" {
|
|
out = append(out, linkedAccount{Provider: tag, Subject: s})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// fields turns a space-separated tag list into a set.
|
|
func fields(list string) map[string]bool {
|
|
m := map[string]bool{}
|
|
for _, f := range strings.Fields(list) {
|
|
m[f] = true
|
|
}
|
|
return m
|
|
}
|