Skip to content

Commit 4a7d97e

Browse files
authored
feat: add the ability to revoke tokens (#275)
1 parent 5816bca commit 4a7d97e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

auth/device_flow.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,19 @@ func (c Config) PollToken(ctx context.Context, code *DeviceCode) (*Token, *atlas
109109
return token, resp, nil
110110
}
111111
}
112+
113+
// Revoke takes an access or refresh token and revokes it.
114+
func (c Config) Revoke(ctx context.Context, token, tokenTypeHint string) (*atlas.Response, error) {
115+
req, err := c.NewRequest(ctx, http.MethodPost, deviceBasePath+"/revoke",
116+
url.Values{
117+
"client_id": {c.ClientID},
118+
"token": {token},
119+
"token_type_hint": {tokenTypeHint},
120+
},
121+
)
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
return c.Do(ctx, req, nil)
127+
}

auth/device_flow_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestConfig_RequestCode(t *testing.T) {
2727
defer teardown()
2828

2929
mux.HandleFunc("/api/private/unauth/account/device/authorize", func(w http.ResponseWriter, r *http.Request) {
30-
testMethod(t, r, http.MethodPost)
30+
testMethod(t, r)
3131
fmt.Fprintf(w, `{
3232
"user_code": "QW3PYV7R",
3333
"verification_uri": "%s/account/connect",
@@ -60,7 +60,7 @@ func TestConfig_GetToken(t *testing.T) {
6060
defer teardown()
6161

6262
mux.HandleFunc("/api/private/unauth/account/device/token", func(w http.ResponseWriter, r *http.Request) {
63-
testMethod(t, r, http.MethodPost)
63+
testMethod(t, r)
6464
fmt.Fprint(w, `{
6565
"access_token": "secret1",
6666
"refresh_token": "secret2",
@@ -99,7 +99,7 @@ func TestConfig_PollToken(t *testing.T) {
9999
defer teardown()
100100

101101
mux.HandleFunc("/api/private/unauth/account/device/token", func(w http.ResponseWriter, r *http.Request) {
102-
testMethod(t, r, http.MethodPost)
102+
testMethod(t, r)
103103
fmt.Fprint(w, `{
104104
"access_token": "secret1",
105105
"refresh_token": "secret2",
@@ -132,3 +132,17 @@ func TestConfig_PollToken(t *testing.T) {
132132
t.Error(diff)
133133
}
134134
}
135+
136+
func TestConfig_Revoke(t *testing.T) {
137+
config, mux, teardown := setup()
138+
defer teardown()
139+
140+
mux.HandleFunc("/api/private/unauth/account/device/revoke", func(w http.ResponseWriter, r *http.Request) {
141+
testMethod(t, r)
142+
})
143+
144+
_, err := config.Revoke(ctx, "a", "refresh_token")
145+
if err != nil {
146+
t.Fatalf("RequestCode returned error: %v", err)
147+
}
148+
}

auth/oauth_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ func setup() (config *Config, mux *http.ServeMux, teardown func()) {
7070
return config, mux, server.Close
7171
}
7272

73-
func testMethod(t *testing.T, r *http.Request, expected string) {
73+
func testMethod(t *testing.T, r *http.Request) {
7474
t.Helper()
75-
if expected != r.Method {
76-
t.Errorf("Request method = %v, expected %v", r.Method, expected)
75+
if http.MethodPost != r.Method {
76+
t.Errorf("Request method = %v, expected %v", r.Method, http.MethodPost)
7777
}
7878
}
7979

0 commit comments

Comments
 (0)