mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: allow external services to be authable (#9996)
* feat: allow external services to be authable * Refactor external auth config structure for defaults * Add support for new config properties * Change the name of external auth * Move externalauth -> external-auth * Run gen * Fix tests * Fix MW tests * Fix git auth redirect * Fix lint * Fix name * Allow any ID * Fix invalid type test * Fix e2e tests * Fix comments * Fix colors * Allow accepting any type as string * Run gen * Fix href
This commit is contained in:
@@ -7,10 +7,39 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// EnhancedExternalAuthProvider is a constant that represents enhanced
|
||||
// support for a type of external authentication. All of the Git providers
|
||||
// are examples of enhanced, because they support intercepting "git clone".
|
||||
type EnhancedExternalAuthProvider string
|
||||
|
||||
func (e EnhancedExternalAuthProvider) String() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
// Git returns whether the provider is a Git provider.
|
||||
func (e EnhancedExternalAuthProvider) Git() bool {
|
||||
switch e {
|
||||
case EnhancedExternalAuthProviderGitHub,
|
||||
EnhancedExternalAuthProviderGitLab,
|
||||
EnhancedExternalAuthProviderBitBucket,
|
||||
EnhancedExternalAuthProviderAzureDevops:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
EnhancedExternalAuthProviderAzureDevops EnhancedExternalAuthProvider = "azure-devops"
|
||||
EnhancedExternalAuthProviderGitHub EnhancedExternalAuthProvider = "github"
|
||||
EnhancedExternalAuthProviderGitLab EnhancedExternalAuthProvider = "gitlab"
|
||||
EnhancedExternalAuthProviderBitBucket EnhancedExternalAuthProvider = "bitbucket"
|
||||
)
|
||||
|
||||
type ExternalAuth struct {
|
||||
Authenticated bool `json:"authenticated"`
|
||||
Device bool `json:"device"`
|
||||
Type string `json:"type"`
|
||||
DisplayName string `json:"display_name"`
|
||||
|
||||
// User is the user that authenticated with the provider.
|
||||
User *ExternalAuthUser `json:"user"`
|
||||
@@ -50,7 +79,7 @@ type ExternalAuthDeviceExchange struct {
|
||||
}
|
||||
|
||||
func (c *Client) ExternalAuthDeviceByID(ctx context.Context, provider string) (ExternalAuthDevice, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/externalauth/%s/device", provider), nil)
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/external-auth/%s/device", provider), nil)
|
||||
if err != nil {
|
||||
return ExternalAuthDevice{}, err
|
||||
}
|
||||
@@ -64,7 +93,7 @@ func (c *Client) ExternalAuthDeviceByID(ctx context.Context, provider string) (E
|
||||
|
||||
// ExchangeGitAuth exchanges a device code for an external auth token.
|
||||
func (c *Client) ExternalAuthDeviceExchange(ctx context.Context, provider string, req ExternalAuthDeviceExchange) error {
|
||||
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/externalauth/%s/device", provider), req)
|
||||
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/external-auth/%s/device", provider), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -77,7 +106,7 @@ func (c *Client) ExternalAuthDeviceExchange(ctx context.Context, provider string
|
||||
|
||||
// ExternalAuthByID returns the external auth for the given provider by ID.
|
||||
func (c *Client) ExternalAuthByID(ctx context.Context, provider string) (ExternalAuth, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/externalauth/%s", provider), nil)
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/external-auth/%s", provider), nil)
|
||||
if err != nil {
|
||||
return ExternalAuth{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user