forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RevokeAccessUserTokens and RevokeZoneLevelAccessUserTokens
- Loading branch information
Petr Tichý
committed
Jan 24, 2022
1 parent
2bd8514
commit 88c1a40
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type AccessUserEmail struct { | ||
Email string `json:"email"` | ||
} | ||
|
||
// RevokeAccessUserTokens revokes any outstanding tokens issued for a specific user | ||
// Access User. | ||
// | ||
// API reference: https://api.cloudflare.com/#access-organizations-revoke-all-access-tokens-for-a-user | ||
func (api *API) RevokeAccessUserTokens(ctx context.Context, accountID string, accessUserEmail AccessUserEmail) error { | ||
return api.revokeUserTokens(ctx, accountID, accessUserEmail, AccountRouteRoot) | ||
} | ||
|
||
// RevokeZoneLevelAccessUserTokens revokes any outstanding tokens issued for a specific user | ||
// Access User. | ||
// | ||
// API reference: https://api.cloudflare.com/#zone-level-access-organizations-revoke-all-access-tokens-for-a-user | ||
func (api *API) RevokeZoneLevelAccessUserTokens(ctx context.Context, zoneID string, accessUserEmail AccessUserEmail) error { | ||
return api.revokeUserTokens(ctx, zoneID, accessUserEmail, ZoneRouteRoot) | ||
} | ||
|
||
func (api *API) revokeUserTokens(ctx context.Context, id string, accessUserEmail AccessUserEmail, routeRoot RouteRoot) error { | ||
uri := fmt.Sprintf("/%s/%s/access/organizations/revoke_user", routeRoot, id) | ||
|
||
_, err := api.makeRequestContext(ctx, http.MethodPost, uri, accessUserEmail) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestRevokeUserTokens(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"result": true | ||
} | ||
`) | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/organizations/revoke_user", handler) | ||
|
||
AccessUserEmail := AccessUserEmail{Email: "[email protected]"} | ||
|
||
err := client.RevokeAccessUserTokens(context.Background(), testAccountID, AccessUserEmail) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestZoneLevelRevokeUserTokens(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"result": true | ||
} | ||
`) | ||
} | ||
|
||
mux.HandleFunc("/zones/"+testZoneID+"/access/organizations/revoke_user", handler) | ||
|
||
AccessUserEmail := AccessUserEmail{Email: "[email protected]"} | ||
|
||
err := client.RevokeZoneLevelAccessUserTokens(context.Background(), testZoneID, AccessUserEmail) | ||
|
||
assert.NoError(t, err) | ||
} |