File size: 4,276 Bytes
0eec4dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os, sys, tempfile, time
os.environ["HUB_SERVER_DATA"] = tempfile.mkdtemp()
os.environ["DEV_LOGIN"] = "1"
os.environ["BASE_URL"] = "http://localhost:5000"
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "server"))
import pyotp, pytest
import app as A
import security as sec


def client():
    return A.app.test_client()


def dev_login(c, email):
    return c.post("/auth/dev", data={"email": email}, base_url="http://localhost:5000")


def J(c, method, url, body=None):
    return getattr(c, method)(url, json=body if body is not None else {}, base_url="http://localhost:5000")


def test_domain_rules():
    ok = ["a.b@indusface.com", "A@INDUSFACE.COM"]
    bad = ["a@gmail.com", "a@indusface.com.evil.com", "a@evil-indusface.com", "a@sub.indusface.com", "x@indusface.co",
           "a_#EXT#@indusface.com", "", "a@indusface.com@x.com", "@indusface.com"]
    assert all(sec.allowed_email(e) for e in ok)
    assert not any(sec.allowed_email(e) for e in bad)


def test_other_domain_rejected():
    c = client()
    r = dev_login(c, "bob@gmail.com")
    assert "auth_error=domain" in r.headers["Location"]
    assert c.get("/api/state").get_json()["stage"] == "anon"


def test_full_flow_new_user_then_new_device():
    c1 = client()
    s = c1.get("/api/state").get_json()
    assert s["device"]["known"] is False and s["stage"] == "anon"
    dev_login(c1, "asha@indusface.com")
    assert c1.get("/api/state").get_json()["stage"] == "enroll"
    assert J(c1, "get", "/api/devices").status_code == 401            # not ready yet
    e = J(c1, "post", "/api/auth/enroll/start").get_json()
    assert e["qr"].startswith("data:image/svg+xml") and len(e["secret"]) >= 16
    assert J(c1, "post", "/api/auth/enroll/verify", {"code": "000000"}).status_code == 400
    code = pyotp.TOTP(e["secret"]).now()
    r = J(c1, "post", "/api/auth/enroll/verify", {"code": code})
    assert r.status_code == 200 and "hub_device" in r.headers.get("Set-Cookie", "")
    assert c1.get("/api/state").get_json()["stage"] == "ready"
    assert J(c1, "get", "/api/devices").get_json()["devices"][0]["current"]
    # logout, then known device: Microsoft login again, no OTP needed
    J(c1, "post", "/api/auth/logout")
    st = c1.get("/api/state").get_json()
    assert st["stage"] == "anon" and st["device"]["known"] and st["device"]["hint"] == "a***@indusface.com"
    dev_login(c1, "asha@indusface.com")
    assert c1.get("/api/state").get_json()["stage"] == "ready"
    # same account from a NEW device: needs OTP
    c2 = client()
    dev_login(c2, "asha@indusface.com")
    assert c2.get("/api/state").get_json()["stage"] == "otp"
    assert J(c2, "post", "/api/auth/otp", {"code": "123456"}).status_code == 400
    time.sleep(0)  # replay of the code already used at enrolment must be refused
    assert J(c2, "post", "/api/auth/otp", {"code": code}).status_code == 400
    # a fresh code in the next window is accepted
    nxt = pyotp.TOTP(e["secret"]).at(time.time() + 30)
    assert J(c2, "post", "/api/auth/otp", {"code": nxt}).status_code == 200
    assert c2.get("/api/state").get_json()["stage"] == "ready"
    assert len(J(c2, "get", "/api/devices").get_json()["devices"]) == 2


def test_device_bound_to_other_account():
    c = client()
    dev_login(c, "one@indusface.com")
    e = J(c, "post", "/api/auth/enroll/start").get_json()
    J(c, "post", "/api/auth/enroll/verify", {"code": pyotp.TOTP(e["secret"]).now()})
    J(c, "post", "/api/auth/logout")
    r = dev_login(c, "two@indusface.com")
    assert "device_owner" in r.headers["Location"]


def test_lockout_after_five_failures():
    c = client()
    dev_login(c, "lock@indusface.com")
    J(c, "post", "/api/auth/enroll/start")
    for _ in range(5):
        J(c, "post", "/api/auth/enroll/verify", {"code": "111111"})
    assert J(c, "post", "/api/auth/enroll/verify", {"code": "111111"}).status_code == 429


def test_api_needs_json_and_session():
    c = client()
    assert c.post("/api/auth/logout", data="x", base_url="http://localhost:5000").status_code == 415
    assert J(c, "post", "/api/ai/spec", {"question": "x"}).status_code == 401


def test_dev_login_blocked_off_localhost():
    c = client()
    assert c.get("/auth/dev", base_url="http://evil.example.com").status_code == 404