MCPcopy Index your code
hub / github.com/coder/coder / ExternalLogin

Method ExternalLogin

coderd/coderdtest/oidctest/idp.go:697–730  ·  view source on GitHub ↗

ExternalLogin does the oauth2 flow for external auth providers. This requires an authenticated coder client.

(t testing.TB, client *codersdk.Client, opts ...codersdk.RequestOption)

Source from the content-addressed store, hash-verified

695// ExternalLogin does the oauth2 flow for external auth providers. This requires
696// an authenticated coder client.
697func (f *FakeIDP) ExternalLogin(t testing.TB, client *codersdk.Client, opts ...codersdk.RequestOption) {
698 coderOauthURL, err := client.URL.Parse(fmt.Sprintf("/external-auth/%s/callback", f.externalProviderID))
699 require.NoError(t, err)
700 f.SetRedirect(t, coderOauthURL.String())
701
702 cli := f.HTTPClient(client.HTTPClient)
703 cli.CheckRedirect = func(req *http.Request, _ []*http.Request) error {
704 // Store the idTokenClaims to the specific state request. This ties
705 // the claims 1:1 with a given authentication flow.
706 state := req.URL.Query().Get("state")
707 f.stateToIDTokenClaims.Store(state, jwt.MapClaims{})
708 return nil
709 }
710
711 ctx, cancel := context.WithCancel(context.Background())
712 t.Cleanup(cancel)
713 req, err := http.NewRequestWithContext(ctx, "GET", coderOauthURL.String(), nil)
714 require.NoError(t, err)
715 // External auth flow requires the user be authenticated.
716 opts = append([]codersdk.RequestOption{client.SessionTokenProvider.AsRequestOption()}, opts...)
717 if cli.Jar == nil {
718 cli.Jar, err = cookiejar.New(nil)
719 require.NoError(t, err, "failed to create cookie jar")
720 }
721
722 for _, opt := range opts {
723 opt(req)
724 }
725
726 res, err := cli.Do(req)
727 require.NoError(t, err)
728 require.Equal(t, http.StatusOK, res.StatusCode, "client failed to login")
729 _ = res.Body.Close()
730}
731
732// DeviceLogin does the oauth2 device flow for external auth providers.
733func (*FakeIDP) DeviceLogin(t testing.TB, client *codersdk.Client, externalAuthID string) {

Callers 3

TestExternalAuthByIDFunction · 0.95

Calls 12

SetRedirectMethod · 0.95
HTTPClientMethod · 0.95
ParseMethod · 0.65
GetMethod · 0.65
CleanupMethod · 0.65
AsRequestOptionMethod · 0.65
NewMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45
StoreMethod · 0.45
EqualMethod · 0.45

Tested by 3

TestExternalAuthByIDFunction · 0.76