Skip to content

Commit

Permalink
Add RevokeAccessUserTokens and RevokeZoneLevelAccessUserTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Tichý committed Jan 24, 2022
1 parent 2bd8514 commit 88c1a40
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
38 changes: 38 additions & 0 deletions access_user_tokens.go
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
}
55 changes: 55 additions & 0 deletions access_user_tokens_test.go
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)
}

0 comments on commit 88c1a40

Please sign in to comment.