Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Add support for Anomaly endpoints #214

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions management/anomaly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package management

import (
"net/http"
)

type AnomalyManager struct {
*Management
}

func newAnomalyManager(m *Management) *AnomalyManager {
return &AnomalyManager{m}
}

// Check if a given IP address is blocked via the multiple user accounts
// trigger due to multiple failed logins.
//
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/get_ips_by_id
func (m *AnomalyManager) CheckIP(ip string, opts ...RequestOption) (isBlocked bool, err error) {
req, err := m.NewRequest("GET", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
if err != nil {
return false, err
}

res, err := m.Do(req)
if err != nil {
return false, err
}

// 200: IP address specified is currently blocked.
if res.StatusCode == http.StatusOK {
return true, nil
}

// 404: IP address specified is not currently blocked.
if res.StatusCode == http.StatusNotFound {
return false, nil
}

return false, newError(res.Body)
}

// Unblock an IP address currently blocked by the multiple user accounts
// trigger due to multiple failed logins.
//
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/delete_ips_by_id
func (m *AnomalyManager) UnblockIP(ip string, opts ...RequestOption) (err error) {
return m.Request("DELETE", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
}
27 changes: 27 additions & 0 deletions management/anomaly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package management

import (
"fmt"
"testing"
)

func TestAnomaly(t *testing.T) {

t.Run("CheckIP", func(t *testing.T) {
isBlocked, err := m.Anomaly.CheckIP("1.1.1.1")
if err != nil {
t.Error(err)
}
if isBlocked {
t.Error(fmt.Errorf("IP should not be blocked"))
}
})

t.Run("UnblockIP", func(t *testing.T) {
err := m.Anomaly.UnblockIP("1.1.1.1")
if err != nil {
t.Error(err)
}
})

}
4 changes: 4 additions & 0 deletions management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ type Management struct {
// SigningKey manages Auth0 Application Signing Keys.
SigningKey *SigningKeyManager

// Anomaly manages the IP blocks
Anomaly *AnomalyManager

url *url.URL
basePath string
userAgent string
Expand Down Expand Up @@ -223,6 +226,7 @@ func New(domain string, options ...ManagementOption) (*Management, error) {
m.Prompt = newPromptManager(m)
m.Blacklist = newBlacklistManager(m)
m.SigningKey = newSigningKeyManager(m)
m.Anomaly = newAnomalyManager(m)

return m, nil
}
Expand Down