Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad53435

Browse files
committed
test: add unit test to use oauth2 client and revocation
1 parent5cb397f commitad53435

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

‎coderd/oauth2_test.go‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func TestOAuth2ProviderRevoke(t *testing.T) {
720720
},
721721
},
722722
{
723-
name:"DeleteToken",
723+
name:"DeleteApp",
724724
fn:func(ctx context.Context,client*codersdk.Client,sexchangeSetup) {
725725
err:=client.RevokeOAuth2ProviderApp(ctx,s.app.ID)
726726
require.NoError(t,err)
@@ -1603,5 +1603,81 @@ func TestOAuth2RegistrationAccessToken(t *testing.T) {
16031603
})
16041604
}
16051605

1606+
// TestOAuth2CoderClient verfies a codersdk client can be used with an oauth client.
1607+
funcTestOAuth2CoderClient(t*testing.T) {
1608+
t.Parallel()
1609+
1610+
owner:=coderdtest.New(t,nil)
1611+
first:=coderdtest.CreateFirstUser(t,owner)
1612+
1613+
// Setup an oauth app
1614+
ctx:=testutil.Context(t,testutil.WaitLong)
1615+
app,err:=owner.PostOAuth2ProviderApp(ctx, codersdk.PostOAuth2ProviderAppRequest{
1616+
Name:"new-app",
1617+
CallbackURL:"http://localhost",
1618+
})
1619+
require.NoError(t,err)
1620+
1621+
appsecret,err:=owner.PostOAuth2ProviderAppSecret(ctx,app.ID)
1622+
require.NoError(t,err)
1623+
1624+
cfg:=&oauth2.Config{
1625+
ClientID:app.ID.String(),
1626+
ClientSecret:appsecret.ClientSecretFull,
1627+
Endpoint: oauth2.Endpoint{
1628+
AuthURL:app.Endpoints.Authorization,
1629+
DeviceAuthURL:app.Endpoints.DeviceAuth,
1630+
TokenURL:app.Endpoints.Token,
1631+
AuthStyle:oauth2.AuthStyleInParams,
1632+
},
1633+
RedirectURL:app.CallbackURL,
1634+
Scopes: []string{},
1635+
}
1636+
1637+
// Make a new user
1638+
client,user:=coderdtest.CreateAnotherUser(t,owner,first.OrganizationID)
1639+
1640+
// Do an OAuth2 token exchange and get a new client with an oauth token
1641+
state:=uuid.NewString()
1642+
1643+
// Get an OAuth2 code for a token exchange
1644+
code,err:=oidctest.OAuth2GetCode(
1645+
cfg.AuthCodeURL(state),
1646+
func(req*http.Request) (*http.Response,error) {
1647+
// Change to POST to simulate the form submission
1648+
req.Method=http.MethodPost
1649+
1650+
// Prevent automatic redirect following
1651+
client.HTTPClient.CheckRedirect=func(req*http.Request,via []*http.Request)error {
1652+
returnhttp.ErrUseLastResponse
1653+
}
1654+
returnclient.Request(ctx,req.Method,req.URL.String(),nil)
1655+
},
1656+
)
1657+
require.NoError(t,err)
1658+
1659+
token,err:=cfg.Exchange(ctx,code)
1660+
require.NoError(t,err)
1661+
1662+
// Use the oauth client's authentication
1663+
// TODO: The SDK could probably support this with a better syntax/api.
1664+
oauthClient:=oauth2.NewClient(ctx,oauth2.StaticTokenSource(token))
1665+
usingOauth:=codersdk.New(owner.URL)
1666+
usingOauth.HTTPClient=oauthClient
1667+
1668+
me,err:=usingOauth.User(ctx,codersdk.Me)
1669+
require.NoError(t,err)
1670+
require.Equal(t,user.ID,me.ID)
1671+
1672+
// Revoking the refresh token should prevent further access
1673+
// Revoking the refresh also invalidates the associated access token.
1674+
err=usingOauth.RevokeOAuth2Token(ctx,app.ID,token.RefreshToken)
1675+
require.NoError(t,err)
1676+
1677+
_,err=usingOauth.User(ctx,codersdk.Me)
1678+
require.Error(t,err)
1679+
1680+
}
1681+
16061682
// NOTE: OAuth2 client registration validation tests have been migrated to
16071683
// oauth2provider/validation_test.go for better separation of concerns

‎codersdk/oauth2.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"strings"
1011

1112
"github.com/google/uuid"
1213
)
@@ -212,6 +213,26 @@ func (e OAuth2ProviderResponseType) Valid() bool {
212213
returnfalse
213214
}
214215

216+
// RevokeOAuth2Token revokes a specific OAuth2 token using RFC 7009 token revocation.
217+
func (c*Client)RevokeOAuth2Token(ctx context.Context,clientID uuid.UUID,tokenstring)error {
218+
form:= url.Values{}
219+
form.Set("token",token)
220+
// Client authentication is handled via the client_id in the app middleware
221+
form.Set("client_id",clientID.String())
222+
223+
res,err:=c.Request(ctx,http.MethodPost,"/oauth2/revoke",strings.NewReader(form.Encode()),func(r*http.Request) {
224+
r.Header.Set("Content-Type","application/x-www-form-urlencoded")
225+
})
226+
iferr!=nil {
227+
returnerr
228+
}
229+
deferres.Body.Close()
230+
ifres.StatusCode!=http.StatusOK {
231+
returnReadBodyAsError(res)
232+
}
233+
returnnil
234+
}
235+
215236
// RevokeOAuth2ProviderApp completely revokes an app's access for the
216237
// authenticated user.
217238
func (c*Client)RevokeOAuth2ProviderApp(ctx context.Context,appID uuid.UUID)error {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp