2023-11-02 17:27:30 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-13 18:13:34 +02:00
|
|
|
"database/sql"
|
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-11-02 17:27:30 +02:00
|
|
|
|
2023-11-13 18:13:34 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-11-02 17:27:30 +02:00
|
|
|
)
|
|
|
|
|
2023-11-13 18:13:34 +02:00
|
|
|
//go:embed embed/userinfo_by_id.sql
|
|
|
|
var oidcUserInfoQuery string
|
2023-11-02 17:27:30 +02:00
|
|
|
|
2023-11-13 18:13:34 +02:00
|
|
|
func (q *Queries) GetOIDCUserInfo(ctx context.Context, userID string) (_ *OIDCUserInfo, err error) {
|
|
|
|
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
|
|
|
}
|
2023-11-13 18:13:34 +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
|
|
|
}
|
2023-11-13 18:13:34 +02:00
|
|
|
if userInfo.User == nil {
|
|
|
|
return nil, errors.ThrowNotFound(nil, "QUERY-ahs4S", "Errors.User.NotFound")
|
2023-11-02 17:27:30 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 18:13:34 +02:00
|
|
|
return userInfo, nil
|
2023-11-02 17:27:30 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 18:13:34 +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
|
|
|
}
|