-
Notifications
You must be signed in to change notification settings - Fork 1
clients/v1: add service access token methods #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package sams | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"connectrpc.com/connect" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
clientsv1 "github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1" | ||
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1/clientsv1connect" | ||
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/scopes" | ||
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/services" | ||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// ServiceAccessTokensServiceV1 provides client methods to interact with the | ||
// ServiceAccessTokensService API v1. | ||
type ServiceAccessTokensServiceV1 struct { | ||
client *ClientV1 | ||
} | ||
|
||
func (s *ServiceAccessTokensServiceV1) newClient(ctx context.Context) clientsv1connect.ServiceAccessTokensServiceClient { | ||
return clientsv1connect.NewServiceAccessTokensServiceClient( | ||
oauth2.NewClient(ctx, s.client.tokenSource), | ||
s.client.gRPCURL(), | ||
connect.WithInterceptors(s.client.defaultInterceptors...), | ||
) | ||
} | ||
|
||
// CreateServiceAccessTokenOptions represents the optional parameters for creating a service access token. | ||
type CreateServiceAccessTokenOptions struct { | ||
// The human-friendly name of the token (optional). | ||
DisplayName string | ||
// The time the token will expire (optional, defaults to never expire). | ||
ExpiresAt *time.Time | ||
} | ||
|
||
// CreateServiceAccessTokenResponse represents the response from creating a service access token. | ||
type CreateServiceAccessTokenResponse struct { | ||
Token *clientsv1.ServiceAccessToken | ||
Secret string | ||
} | ||
|
||
// CreateServiceAccessToken creates a new service access token. | ||
// | ||
// Required scope: sams::service_access_tokens::write | ||
func (s *ServiceAccessTokensServiceV1) CreateServiceAccessToken(ctx context.Context, service services.Service, tokenScopes []scopes.Scope, userID string, opts CreateServiceAccessTokenOptions) (*CreateServiceAccessTokenResponse, error) { | ||
if service == "" { | ||
return nil, errors.New("service cannot be empty") | ||
} | ||
if len(tokenScopes) == 0 { | ||
return nil, errors.New("scopes cannot be empty") | ||
} | ||
if userID == "" { | ||
return nil, errors.New("user ID cannot be empty") | ||
} | ||
|
||
token := &clientsv1.ServiceAccessToken{ | ||
Service: string(service), | ||
Scopes: scopes.ToStrings(tokenScopes), | ||
UserId: userID, | ||
DisplayName: opts.DisplayName, | ||
} | ||
|
||
if opts.ExpiresAt != nil { | ||
token.ExpireTime = timestamppb.New(*opts.ExpiresAt) | ||
} | ||
|
||
req := &clientsv1.CreateServiceAccessTokenRequest{Token: token} | ||
client := s.newClient(ctx) | ||
resp, err := parseResponseAndError(client.CreateServiceAccessToken(ctx, connect.NewRequest(req))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CreateServiceAccessTokenResponse{ | ||
Token: resp.Msg.Token, | ||
Secret: resp.Msg.Secret, | ||
}, nil | ||
} | ||
|
||
// ListServiceAccessTokensOptions represents the options for listing service access tokens. | ||
type ListServiceAccessTokensOptions struct { | ||
// Maximum number of results to return (optional). | ||
PageSize int32 | ||
// Page token for pagination (optional). | ||
PageToken string | ||
// Service filter (optional). | ||
Service string | ||
// User ID filter (optional). | ||
UserID string | ||
// Whether to include expired tokens (optional). | ||
ShowExpired bool | ||
} | ||
|
||
// ListServiceAccessTokens returns a list of service access tokens in reverse chronological | ||
// order by creation time. | ||
// | ||
// Required scope: sams::service_access_tokens::read | ||
func (s *ServiceAccessTokensServiceV1) ListServiceAccessTokens(ctx context.Context, opts ListServiceAccessTokensOptions) ([]*clientsv1.ServiceAccessToken, error) { | ||
req := &clientsv1.ListServiceAccessTokensRequest{ | ||
PageSize: opts.PageSize, | ||
PageToken: opts.PageToken, | ||
} | ||
|
||
// Build filters | ||
var filters []*clientsv1.ListServiceAccessTokensFilter | ||
if opts.Service != "" { | ||
filters = append(filters, &clientsv1.ListServiceAccessTokensFilter{ | ||
Filter: &clientsv1.ListServiceAccessTokensFilter_Service{Service: opts.Service}, | ||
}) | ||
} | ||
if opts.UserID != "" { | ||
filters = append(filters, &clientsv1.ListServiceAccessTokensFilter{ | ||
Filter: &clientsv1.ListServiceAccessTokensFilter_UserId{UserId: opts.UserID}, | ||
}) | ||
} | ||
if opts.ShowExpired { | ||
filters = append(filters, &clientsv1.ListServiceAccessTokensFilter{ | ||
Filter: &clientsv1.ListServiceAccessTokensFilter_ShowExpired{ShowExpired: opts.ShowExpired}, | ||
}) | ||
} | ||
req.Filters = filters | ||
|
||
client := s.newClient(ctx) | ||
resp, err := parseResponseAndError(client.ListServiceAccessTokens(ctx, connect.NewRequest(req))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Msg.GetTokens(), nil | ||
} | ||
|
||
// RevokeServiceAccessToken revokes the specified service access token. | ||
// | ||
// Required scope: sams::service_access_tokens::delete | ||
func (s *ServiceAccessTokensServiceV1) RevokeServiceAccessToken(ctx context.Context, tokenID string) error { | ||
if tokenID == "" { | ||
return errors.New("token ID cannot be empty") | ||
} | ||
|
||
req := &clientsv1.RevokeServiceAccessTokenRequest{Id: tokenID} | ||
client := s.newClient(ctx) | ||
_, err := parseResponseAndError(client.RevokeServiceAccessToken(ctx, connect.NewRequest(req))) | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings are now all outdated 😁 assuming this is an intended change.