Skip to content

Commit

Permalink
refactor: use options pattern in RegisterPGPPublicKey
Browse files Browse the repository at this point in the history
Replace the optional parameters on RegisterPGPPublicKey with the options pattern to preserve backwards compatibility on the clients.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
  • Loading branch information
utkuozdemir committed Oct 28, 2022
1 parent c647861 commit 7b80a50
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions pkg/client/auth/auth.go
Expand Up @@ -25,18 +25,39 @@ func NewClient(conn *grpc.ClientConn) *Client {
}
}

// RegisterPGPPublicKeyOption customizes authpb.RegisterPublicKeyRequest.
type RegisterPGPPublicKeyOption func(*authpb.RegisterPublicKeyRequest)

// WithScopes sets the scopes in the authpb.RegisterPublicKeyRequest.
func WithScopes(scopes ...string) RegisterPGPPublicKeyOption {
return func(o *authpb.RegisterPublicKeyRequest) {
o.Scopes = scopes
}
}

// WithSkipUserScopes sets the skipUserScopes flag in the authpb.RegisterPublicKeyRequest.
// If true and no scopes are specified using WithScopes, the scopes of the user are assigned to the public key by the server.
func WithSkipUserScopes(skipUserScopes bool) RegisterPGPPublicKeyOption {
return func(o *authpb.RegisterPublicKeyRequest) {
o.SkipUserScopes = skipUserScopes
}
}

// RegisterPGPPublicKey registers a PGP public key for the given identity and returns the login URL.
// Registered public key will need to be verified before it can be used for signing.
// If no scopes are specified and skipUserScopes is false, the scopes of the user are assigned to the public key by the server.
func (client *Client) RegisterPGPPublicKey(ctx context.Context, email string, publicKey []byte, skipUserScopes bool, scopes ...string) (string, error) {
resp, err := client.conn.RegisterPublicKey(ctx, &authpb.RegisterPublicKeyRequest{
func (client *Client) RegisterPGPPublicKey(ctx context.Context, email string, publicKey []byte, opt ...RegisterPGPPublicKeyOption) (string, error) {
request := authpb.RegisterPublicKeyRequest{
Identity: &authpb.Identity{Email: email},
PublicKey: &authpb.PublicKey{
PgpData: publicKey,
},
Scopes: scopes,
SkipUserScopes: skipUserScopes,
})
}

for _, o := range opt {
o(&request)
}

resp, err := client.conn.RegisterPublicKey(ctx, &request)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 7b80a50

Please sign in to comment.