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

Commit bca4c52

Browse files
committed
Use custom implementation
1 parent 42db3b2 commit bca4c52

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

management/anomaly.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package management
22

3+
import (
4+
"net/http"
5+
)
6+
37
type AnomalyManager struct {
48
*Management
59
}
@@ -12,16 +16,34 @@ func newAnomalyManager(m *Management) *AnomalyManager {
1216
// trigger due to multiple failed logins.
1317
//
1418
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/get_ips_by_id
15-
func (m *AnomalyManager) CheckIP(ip string, opts ...RequestOption) (err error) {
16-
err = m.Request("GET", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
17-
return
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)
1841
}
1942

2043
// Unblock an IP address currently blocked by the multiple user accounts
2144
// trigger due to multiple failed logins.
2245
//
2346
// See: https://auth0.com/docs/api/management/v2#!/Anomaly/delete_ips_by_id
2447
func (m *AnomalyManager) UnblockIP(ip string, opts ...RequestOption) (err error) {
25-
err = m.Request("DELETE", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
26-
return
48+
return m.Request("DELETE", m.URI("anomaly", "blocks", "ips", ip), nil, opts...)
2749
}

management/anomaly_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package management
22

33
import (
4+
"fmt"
45
"testing"
56
)
67

78
func TestAnomaly(t *testing.T) {
89

910
t.Run("CheckIP", func(t *testing.T) {
10-
err := m.Anomaly.CheckIP("1.1.1.1")
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")
1122
if err != nil {
1223
t.Error(err)
1324
}

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)