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

Commit 8ddbd9d

Browse files
authored
Merge pull request #214 from Widcket/feature/anomaly
Add support for Anomaly endpoints
2 parents 4fa0021 + d6b669c commit 8ddbd9d

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

management/anomaly.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package management
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type AnomalyManager struct {
8+
*Management
9+
}
10+
11+
func newAnomalyManager(m *Management) *AnomalyManager {
12+
return &AnomalyManager{m}
13+
}
14+
15+
// Check if a given IP address is blocked via the multiple user accounts
16+
// trigger due to multiple failed logins.
17+
//
18+
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/get_ips_by_id
19+
func (m *AnomalyManager) CheckIP(ip string, opts ...RequestOption) (isBlocked bool, err error) {
20+
req, err := m.NewRequest("GET", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
21+
if err != nil {
22+
return false, err
23+
}
24+
25+
res, err := m.Do(req)
26+
if err != nil {
27+
return false, err
28+
}
29+
30+
// 200: IP address specified is currently blocked.
31+
if res.StatusCode == http.StatusOK {
32+
return true, nil
33+
}
34+
35+
// 404: IP address specified is not currently blocked.
36+
if res.StatusCode == http.StatusNotFound {
37+
return false, nil
38+
}
39+
40+
return false, newError(res.Body)
41+
}
42+
43+
// Unblock an IP address currently blocked by the multiple user accounts
44+
// trigger due to multiple failed logins.
45+
//
46+
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/delete_ips_by_id
47+
func (m *AnomalyManager) UnblockIP(ip string, opts ...RequestOption) (err error) {
48+
return m.Request("DELETE", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
49+
}

management/anomaly_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package management
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestAnomaly(t *testing.T) {
9+
10+
t.Run("CheckIP", func(t *testing.T) {
11+
isBlocked, err := m.Anomaly.CheckIP("1.1.1.1")
12+
if err != nil {
13+
t.Error(err)
14+
}
15+
if isBlocked {
16+
t.Error(fmt.Errorf("IP should not be blocked"))
17+
}
18+
})
19+
20+
t.Run("UnblockIP", func(t *testing.T) {
21+
err := m.Anomaly.UnblockIP("1.1.1.1")
22+
if err != nil {
23+
t.Error(err)
24+
}
25+
})
26+
27+
}

management/management.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ type Management struct {
156156
// SigningKey manages Auth0 Application Signing Keys.
157157
SigningKey *SigningKeyManager
158158

159+
// Anomaly manages the IP blocks
160+
Anomaly *AnomalyManager
161+
159162
url *url.URL
160163
basePath string
161164
userAgent string
@@ -223,6 +226,7 @@ func New(domain string, options ...ManagementOption) (*Management, error) {
223226
m.Prompt = newPromptManager(m)
224227
m.Blacklist = newBlacklistManager(m)
225228
m.SigningKey = newSigningKeyManager(m)
229+
m.Anomaly = newAnomalyManager(m)
226230

227231
return m, nil
228232
}

0 commit comments

Comments
 (0)