Documentation ¶
Index ¶
- Constants
- Variables
- func ClientCredentialsTokenSource(conn ConnConfig, clientID, clientSecret string, requestScopes []scopes.Scope) oauth2.TokenSource
- func NewAccountsV1(config AccountsV1Config) (*accountsv1.Client, error)
- func NewNotificationsV1Subscriber(logger log.Logger, opts notificationsv1.SubscriberOptions) (background.Routine, error)
- type AccountsV1Config
- type ClientV1
- type ClientV1Config
- type ConnConfig
- type IntrospectTokenResponse
- type NotificationsV1SubscriberConfig
- type RegisterResourcesMetadata
- type RolesServiceV1
- type SessionsServiceV1
- type TokensServiceV1
- type UsersServiceV1
- func (s *UsersServiceV1) GetUserByEmail(ctx context.Context, email string) (*clientsv1.User, error)
- func (s *UsersServiceV1) GetUserByID(ctx context.Context, id string) (*clientsv1.User, error)
- func (s *UsersServiceV1) GetUserMetadata(ctx context.Context, userID string, namespaces []string) ([]*clientsv1.UserServiceMetadata, error)
- func (s *UsersServiceV1) GetUserRolesByID(ctx context.Context, userID, service string) ([]*clientsv1.Role, error)
- func (s *UsersServiceV1) GetUsersByIDs(ctx context.Context, ids []string) ([]*clientsv1.User, error)
- func (s *UsersServiceV1) UpdateUserMetadata(ctx context.Context, userID, namespace string, metadata map[string]any) (*clientsv1.UserServiceMetadata, error)
Constants ¶
const DefaultExternalURL = "https://accounts.sourcegraph.com"
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrRecordMismatch = errors.New("record mismatch") // ErrAborted is returned due to a concurrency conflict. // e.g. Two clients trying to perform an operation at the same time for the same resource. // It is safe to retry the request at a later time. ErrAborted = errors.New("aborted") )
Functions ¶
func ClientCredentialsTokenSource ¶
func ClientCredentialsTokenSource(conn ConnConfig, clientID, clientSecret string, requestScopes []scopes.Scope) oauth2.TokenSource
ClientCredentialsTokenSource returns a TokenSource that generates an access token using the client credentials flow. Internally, the token returned is reused. So that new tokens are only created when needed. (Provided this `Client` is long-lived.)
The `requestScopes` is a list of scopes to be requested for this token source. Scopes should be defined using the available scopes package. All requested scopes must be allowed by the registered client - see: https://sourcegraph.notion.site/6cc4a1bd9cb247eea9674dbf9d5ce8c3
func NewAccountsV1 ¶
func NewAccountsV1(config AccountsV1Config) (*accountsv1.Client, error)
NewAccountsV1 returns a new SAMS client for interacting with Accounts API V1.
func NewNotificationsV1Subscriber ¶
func NewNotificationsV1Subscriber(logger log.Logger, opts notificationsv1.SubscriberOptions) (background.Routine, error)
NewNotificationsV1Subscriber returns a new background routine for receiving SAMS notifications with v1 protocol.
Types ¶
type AccountsV1Config ¶
type AccountsV1Config struct { ConnConfig // TokenSource is the OAuth2 token source to use for authentication. It MUST be // based on a per-user token that is on behalf of a SAMS user. // // If you have the SAMS user's refresh token, using the oauth2.TokenSource // abstraction will take care of creating short-lived access tokens and refresh // as needed. But if you only have the access token, you will need to use a // StaticTokenSource instead. TokenSource oauth2.TokenSource }
func (AccountsV1Config) Validate ¶
func (c AccountsV1Config) Validate() error
type ClientV1 ¶
type ClientV1 struct {
// contains filtered or unexported fields
}
ClientV1 provides helpers to talk to a SAMS instance via Clients API v1.
func NewClientV1 ¶
func NewClientV1(config ClientV1Config) (*ClientV1, error)
NewClientV1 returns a new SAMS client for interacting with Clients API v1 using the given client credentials, and the scopes are used to as requested scopes for access tokens that are issued to this client.
func (*ClientV1) Roles ¶
func (c *ClientV1) Roles() *RolesServiceV1
func (*ClientV1) Sessions ¶
func (c *ClientV1) Sessions() *SessionsServiceV1
Sessions returns a client handler to interact with the SessionsServiceV1 API.
func (*ClientV1) Tokens ¶
func (c *ClientV1) Tokens() *TokensServiceV1
Tokens returns a client handler to interact with the TokensServiceV1 API.
func (*ClientV1) Users ¶
func (c *ClientV1) Users() *UsersServiceV1
Users returns a client handler to interact with the UsersServiceV1 API.
type ClientV1Config ¶
type ClientV1Config struct { ConnConfig // TokenSource is the OAuth2 token source to use for authentication. It MUST be // based on a per-client token that is on behalf of a SAMS client (i.e. Clients // Credentials). // // The oauth2.TokenSource abstraction will take care of creating short-lived // access tokens as needed. TokenSource oauth2.TokenSource // SessionsCacheSize is the number of sessions to cache in memory. // // The default of 0 (or less) disables caching. SessionsCacheSize int // IntrospectTokenCacheSize is the number of token introspection results to // cache in memory. // // The default of 0 (or less) disables caching. IntrospectTokenCacheSize int }
func (ClientV1Config) Validate ¶
func (c ClientV1Config) Validate() error
type ConnConfig ¶
type ConnConfig struct { // ExternalURL is the configured default external ExternalURL of the relevant Sourcegraph // Accounts instance. ExternalURL string // APIURL is the URL to use for Sourcegraph Accounts API interactions. This // can be set to some internal URLs for private networking. If this is nil, // the client will fall back to ExternalURL instead. APIURL *string }
ConnConfig is the basic configuration for connecting to a Sourcegraph Accounts instance. Callers SHOULD use sams.NewConnConfigFromEnv(...) to construct this where possible to ensure connection configuration is unified across SAMS clients for ease of operation by Core Services team.
func NewConnConfigFromEnv ¶
func NewConnConfigFromEnv(env envGetter) ConnConfig
NewConnConfigFromEnv initializes configuration for connecting to Sourcegraph Accounts using default standards for loading environment variables. This allows the Core Services team to more easily configure access.
func (ConnConfig) Validate ¶
func (c ConnConfig) Validate() error
type IntrospectTokenResponse ¶
type IntrospectTokenResponse struct { // Active indicates whether the token is currently active. The value is "true" // if the token has been issued by the SAMS instance, has not been revoked, and // has not expired. Active bool // Scopes is the list of scopes granted by the token. Scopes scopes.Scopes // ClientID is the identifier of the SAMS client that the token was issued to. ClientID string // ExpiresAt indicates when the token expires. ExpiresAt time.Time }
type NotificationsV1SubscriberConfig ¶
type NotificationsV1SubscriberConfig struct { // ProjectID is the GCP project ID that the Pub/Sub subscription belongs to. It // is almost always the same GCP project that the Cloud Run service is deployed // to. ProjectID string // SubscriptionID is the GCP Pub/Sub subscription ID to receive SAMS // notifications from. SubscriptionID string }
NotificationsV1SubscriberConfig holds configuration for the SAMS notifications that are derived from the environment variables, HOWEVER, this is not a complete configuration to create a notification subscriber.
func NewNotificationsV1SubscriberConfigFromEnv ¶
func NewNotificationsV1SubscriberConfigFromEnv(env envGetter) NotificationsV1SubscriberConfig
NewNotificationsV1SubscriberConfigFromEnv initializes configuration based on environment variables.
type RegisterResourcesMetadata ¶
type RegisterResourcesMetadata struct {
ResourceType roles.ResourceType
}
RegisterResourcesMetadata is the metadata for a set of resources to be registered.
type RolesServiceV1 ¶
type RolesServiceV1 struct {
// contains filtered or unexported fields
}
RolesServiceV1 provides client methods to interact with the RolesService API v1.
func (*RolesServiceV1) RegisterRoleResources ¶
func (s *RolesServiceV1) RegisterRoleResources(ctx context.Context, metadata RegisterResourcesMetadata, resourcesIterator func() ([]*clientsv1.RoleResource, error)) (uint64, error)
RegisterRoleResources registers the resources for a given resource type. `resourcesIterator` is a function that returns a page of resources to register. The function is invoked repeatedly until it produces an empty slice or an error. If another replica is already registering resources for the same resource type the function will return 0 with ErrAborted. ErrAborted means the request is safe to retry at a later time.
Required scope: sams::roles.resources::write
type SessionsServiceV1 ¶
type SessionsServiceV1 struct {
// contains filtered or unexported fields
}
SessionsServiceV1 provides client methods to interact with the SessionsService API v1.
func (*SessionsServiceV1) GetSessionByID ¶
func (s *SessionsServiceV1) GetSessionByID(ctx context.Context, id string) (*clientsv1.Session, error)
GetSessionByID returns the SAMS session with the given ID. It returns ErrNotFound if no such session exists. The session's `User` field is populated if the session is authenticated by a user.
Required scope: sams::session::read
func (*SessionsServiceV1) SignOutSession ¶
func (s *SessionsServiceV1) SignOutSession(ctx context.Context, sessionID, userID string) error
SignOutSession revokes the authenticated state of the session with the given ID for the given user. It does not return error if the session does not exist or is not authenticated. It returns ErrRecordMismatch if the session is authenticated by a different user than the given user.
Required scope: sams::session::write
type TokensServiceV1 ¶
type TokensServiceV1 struct {
// contains filtered or unexported fields
}
TokensServiceV1 provides client methods to interact with the TokensService API v1.
func (*TokensServiceV1) IntrospectToken ¶
func (s *TokensServiceV1) IntrospectToken(ctx context.Context, token string) (*IntrospectTokenResponse, error)
IntrospectToken takes a SAMS access token and returns relevant metadata.
🚨SECURITY: SAMS will return a successful result if the token is valid, but is no longer active. It is critical that the caller not honor tokens where `.Active == false`.
type UsersServiceV1 ¶
type UsersServiceV1 struct {
// contains filtered or unexported fields
}
UsersServiceV1 provides client methods to interact with the UsersService API v1.
func (*UsersServiceV1) GetUserByEmail ¶
GetUserByEmail returns the SAMS user with the given verified email. It returns ErrNotFound if no such user exists.
Required scope: profile
func (*UsersServiceV1) GetUserByID ¶
GetUserByID returns the SAMS user with the given ID. It returns ErrNotFound if no such user exists.
Required scope: profile
func (*UsersServiceV1) GetUserMetadata ¶
func (s *UsersServiceV1) GetUserMetadata(ctx context.Context, userID string, namespaces []string) ([]*clientsv1.UserServiceMetadata, error)
GetUserMetadata returns the metadata associated with the given user ID and metadata namespaces.
Required scopes: sams::user.metadata.${NAMESPACE}::read for each of the requested namespaces.
func (*UsersServiceV1) GetUserRolesByID ¶
func (s *UsersServiceV1) GetUserRolesByID(ctx context.Context, userID, service string) ([]*clientsv1.Role, error)
GetUserRolesByID returns all roles that have been assigned to the SAMS user with the given ID and scoped by the service.
Required scopes: sams::user.roles::read
func (*UsersServiceV1) GetUsersByIDs ¶
func (s *UsersServiceV1) GetUsersByIDs(ctx context.Context, ids []string) ([]*clientsv1.User, error)
GetUsersByIDs returns the list of SAMS users matching the provided IDs.
NOTE: It silently ignores any invalid user IDs, i.e. the length of the return slice may be less than the length of the input slice.
Required scopes: profile
func (*UsersServiceV1) UpdateUserMetadata ¶
func (s *UsersServiceV1) UpdateUserMetadata(ctx context.Context, userID, namespace string, metadata map[string]any) (*clientsv1.UserServiceMetadata, error)
UpdateUserMetadata updates the metadata associated with the given user ID and metadata namespace.
Required scopes: sams::user.metadata.${NAMESPACE}::read for the namespace being updated.