zitadel/internal/query/userinfo.go

49 lines
1.3 KiB
Go
Raw Normal View History

2023-11-02 17:27:30 +02:00
package query
import (
"context"
"database/sql"
_ "embed"
"encoding/json"
"fmt"
2023-11-02 17:27:30 +02:00
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/errors"
2023-11-13 19:20:01 +02:00
"github.com/zitadel/zitadel/internal/telemetry/tracing"
2023-11-02 17:27:30 +02:00
)
//go:embed embed/userinfo_by_id.sql
var oidcUserInfoQuery string
2023-11-02 17:27:30 +02:00
func (q *Queries) GetOIDCUserInfo(ctx context.Context, userID string) (_ *OIDCUserInfo, err error) {
2023-11-13 19:20:01 +02:00
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
userInfo := new(OIDCUserInfo)
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
var data []byte
if err := row.Scan(&data); err != nil {
return err
2023-11-02 17:27:30 +02:00
}
return json.Unmarshal(data, userInfo)
}, oidcUserInfoQuery, userID, authz.GetInstance(ctx).InstanceID())
if err != nil {
return nil, fmt.Errorf("get oidc user info: %w", err)
2023-11-02 17:27:30 +02:00
}
if userInfo.User == nil {
return nil, errors.ThrowNotFound(nil, "QUERY-ahs4S", "Errors.User.NotFound")
2023-11-02 17:27:30 +02:00
}
return userInfo, nil
2023-11-02 17:27:30 +02:00
}
type OIDCUserInfo struct {
User *User `json:"user,omitempty"`
Metadata []UserMetadata `json:"metadata,omitempty"`
Org *struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
PrimaryDomain string `json:"primary_domain,omitempty"`
} `json:"org,omitempty"`
2023-11-02 17:27:30 +02:00
}