ExternalLogin does the oauth2 flow for external auth providers. This requires an authenticated coder client.
(t testing.TB, client *codersdk.Client, opts ...codersdk.RequestOption)
| 695 | // ExternalLogin does the oauth2 flow for external auth providers. This requires |
| 696 | // an authenticated coder client. |
| 697 | func (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. |
| 733 | func (*FakeIDP) DeviceLogin(t testing.TB, client *codersdk.Client, externalAuthID string) { |