diff --git a/management/anomaly.go b/management/anomaly.go new file mode 100644 index 0000000..f81bdfb --- /dev/null +++ b/management/anomaly.go @@ -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...) +} diff --git a/management/anomaly_test.go b/management/anomaly_test.go new file mode 100644 index 0000000..64b404d --- /dev/null +++ b/management/anomaly_test.go @@ -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) + } + }) + +} diff --git a/management/management.go b/management/management.go index 351e0a6..b9ab96a 100644 --- a/management/management.go +++ b/management/management.go @@ -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 @@ -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 }