|
| 1 | +from urllib.parse import parse_qs |
| 2 | +from urllib.parse import urlsplit |
| 3 | + |
| 4 | +from joserfc import jwt |
| 5 | + |
| 6 | +from canaille.app import models |
| 7 | +from canaille.oidc.jose import registry |
| 8 | +from canaille.oidc.provider import compute_amr_values |
| 9 | +from tests.core.test_auth_otp import generate_otp |
| 10 | + |
| 11 | +from . import client_credentials |
| 12 | + |
| 13 | + |
| 14 | +def test_amr_password_only(testclient, user, client, server_jwk, backend): |
| 15 | + """Test that AMR contains only 'pwd' for password-only authentication.""" |
| 16 | + testclient.app.config["CANAILLE"]["AUTHENTICATION_FACTORS"] = ["password"] |
| 17 | + |
| 18 | + res = testclient.get("/login") |
| 19 | + res.form["login"] = "user" |
| 20 | + res = res.form.submit().follow() |
| 21 | + res.form["password"] = "correct horse battery staple" |
| 22 | + res = res.form.submit() |
| 23 | + |
| 24 | + res = testclient.get( |
| 25 | + "/oauth/authorize", |
| 26 | + params=dict( |
| 27 | + response_type="code", |
| 28 | + client_id=client.client_id, |
| 29 | + scope="openid", |
| 30 | + nonce="somenonce", |
| 31 | + redirect_uri="https://client.test/redirect1", |
| 32 | + ), |
| 33 | + ) |
| 34 | + res = res.form.submit(name="answer", value="accept", status=302) |
| 35 | + |
| 36 | + params = parse_qs(urlsplit(res.location).query) |
| 37 | + code = params["code"][0] |
| 38 | + |
| 39 | + res = testclient.post( |
| 40 | + "/oauth/token", |
| 41 | + params=dict( |
| 42 | + grant_type="authorization_code", |
| 43 | + code=code, |
| 44 | + scope="openid", |
| 45 | + redirect_uri=client.redirect_uris[0], |
| 46 | + ), |
| 47 | + headers={"Authorization": f"Basic {client_credentials(client)}"}, |
| 48 | + ) |
| 49 | + |
| 50 | + id_token = res.json["id_token"] |
| 51 | + claims = jwt.decode(id_token, server_jwk, registry=registry) |
| 52 | + assert claims.claims["amr"] == ["pwd"] |
| 53 | + |
| 54 | + for consent in backend.query(models.Consent, client=client, subject=user): |
| 55 | + backend.delete(consent) |
| 56 | + |
| 57 | + |
| 58 | +def test_amr_password_and_otp(testclient, user, client, server_jwk, backend): |
| 59 | + """Test that AMR contains 'pwd', 'otp', and 'mfa' for password + OTP authentication.""" |
| 60 | + testclient.app.config["CANAILLE"]["AUTHENTICATION_FACTORS"] = ["password", "otp"] |
| 61 | + |
| 62 | + res = testclient.get("/login") |
| 63 | + res.form["login"] = "user" |
| 64 | + res = res.form.submit().follow() |
| 65 | + res.form["password"] = "correct horse battery staple" |
| 66 | + res = res.form.submit().follow() |
| 67 | + |
| 68 | + totp_period = int( |
| 69 | + testclient.app.config["CANAILLE"]["TOTP_LIFETIME"].total_seconds() |
| 70 | + ) |
| 71 | + res.form["otp"] = generate_otp("TOTP", user.secret_token, totp_period=totp_period) |
| 72 | + res = res.form.submit() |
| 73 | + |
| 74 | + res = testclient.get( |
| 75 | + "/oauth/authorize", |
| 76 | + params=dict( |
| 77 | + response_type="code", |
| 78 | + client_id=client.client_id, |
| 79 | + scope="openid", |
| 80 | + nonce="somenonce", |
| 81 | + redirect_uri="https://client.test/redirect1", |
| 82 | + ), |
| 83 | + ) |
| 84 | + res = res.form.submit(name="answer", value="accept", status=302) |
| 85 | + |
| 86 | + params = parse_qs(urlsplit(res.location).query) |
| 87 | + code = params["code"][0] |
| 88 | + |
| 89 | + res = testclient.post( |
| 90 | + "/oauth/token", |
| 91 | + params=dict( |
| 92 | + grant_type="authorization_code", |
| 93 | + code=code, |
| 94 | + scope="openid", |
| 95 | + redirect_uri=client.redirect_uris[0], |
| 96 | + ), |
| 97 | + headers={"Authorization": f"Basic {client_credentials(client)}"}, |
| 98 | + ) |
| 99 | + |
| 100 | + id_token = res.json["id_token"] |
| 101 | + claims = jwt.decode(id_token, server_jwk, registry=registry) |
| 102 | + assert set(claims.claims["amr"]) == {"pwd", "otp", "mfa"} |
| 103 | + |
| 104 | + for consent in backend.query(models.Consent, client=client, subject=user): |
| 105 | + backend.delete(consent) |
| 106 | + |
| 107 | + |
| 108 | +def test_amr_password_and_sms(smtpd, testclient, user, client, server_jwk, backend): |
| 109 | + """Test that AMR contains 'pwd', 'sms', and 'mfa' for password + SMS authentication.""" |
| 110 | + testclient.app.config["CANAILLE"]["AUTHENTICATION_FACTORS"] = ["password", "sms"] |
| 111 | + |
| 112 | + res = testclient.get("/login") |
| 113 | + res.form["login"] = "user" |
| 114 | + res = res.form.submit().follow() |
| 115 | + res.form["password"] = "correct horse battery staple" |
| 116 | + res = res.form.submit().follow() |
| 117 | + |
| 118 | + backend.reload(user) |
| 119 | + res.form["otp"] = user.one_time_password |
| 120 | + res = res.form.submit() |
| 121 | + |
| 122 | + res = testclient.get( |
| 123 | + "/oauth/authorize", |
| 124 | + params=dict( |
| 125 | + response_type="code", |
| 126 | + client_id=client.client_id, |
| 127 | + scope="openid", |
| 128 | + nonce="somenonce", |
| 129 | + redirect_uri="https://client.test/redirect1", |
| 130 | + ), |
| 131 | + ) |
| 132 | + res = res.form.submit(name="answer", value="accept", status=302) |
| 133 | + |
| 134 | + params = parse_qs(urlsplit(res.location).query) |
| 135 | + code = params["code"][0] |
| 136 | + |
| 137 | + res = testclient.post( |
| 138 | + "/oauth/token", |
| 139 | + params=dict( |
| 140 | + grant_type="authorization_code", |
| 141 | + code=code, |
| 142 | + scope="openid", |
| 143 | + redirect_uri=client.redirect_uris[0], |
| 144 | + ), |
| 145 | + headers={"Authorization": f"Basic {client_credentials(client)}"}, |
| 146 | + ) |
| 147 | + |
| 148 | + id_token = res.json["id_token"] |
| 149 | + claims = jwt.decode(id_token, server_jwk, registry=registry) |
| 150 | + assert set(claims.claims["amr"]) == {"pwd", "sms", "mca", "mfa"} |
| 151 | + |
| 152 | + for consent in backend.query(models.Consent, client=client, subject=user): |
| 153 | + backend.delete(consent) |
| 154 | + |
| 155 | + |
| 156 | +def test_amr_password_and_email(smtpd, testclient, user, client, server_jwk, backend): |
| 157 | + """Test that AMR contains 'pwd', 'mca', and 'mfa' for password + email authentication.""" |
| 158 | + testclient.app.config["CANAILLE"]["AUTHENTICATION_FACTORS"] = [ |
| 159 | + "password", |
| 160 | + "email", |
| 161 | + ] |
| 162 | + |
| 163 | + res = testclient.get("/login") |
| 164 | + res.form["login"] = "user" |
| 165 | + res = res.form.submit().follow() |
| 166 | + res.form["password"] = "correct horse battery staple" |
| 167 | + res = res.form.submit().follow() |
| 168 | + |
| 169 | + backend.reload(user) |
| 170 | + res.form["otp"] = user.one_time_password |
| 171 | + res = res.form.submit() |
| 172 | + |
| 173 | + res = testclient.get( |
| 174 | + "/oauth/authorize", |
| 175 | + params=dict( |
| 176 | + response_type="code", |
| 177 | + client_id=client.client_id, |
| 178 | + scope="openid", |
| 179 | + nonce="somenonce", |
| 180 | + redirect_uri="https://client.test/redirect1", |
| 181 | + ), |
| 182 | + ) |
| 183 | + res = res.form.submit(name="answer", value="accept", status=302) |
| 184 | + |
| 185 | + params = parse_qs(urlsplit(res.location).query) |
| 186 | + code = params["code"][0] |
| 187 | + |
| 188 | + res = testclient.post( |
| 189 | + "/oauth/token", |
| 190 | + params=dict( |
| 191 | + grant_type="authorization_code", |
| 192 | + code=code, |
| 193 | + scope="openid", |
| 194 | + redirect_uri=client.redirect_uris[0], |
| 195 | + ), |
| 196 | + headers={"Authorization": f"Basic {client_credentials(client)}"}, |
| 197 | + ) |
| 198 | + |
| 199 | + id_token = res.json["id_token"] |
| 200 | + claims = jwt.decode(id_token, server_jwk, registry=registry) |
| 201 | + assert set(claims.claims["amr"]) == {"pwd", "mca", "mfa"} |
| 202 | + |
| 203 | + for consent in backend.query(models.Consent, client=client, subject=user): |
| 204 | + backend.delete(consent) |
| 205 | + |
| 206 | + |
| 207 | +def test_compute_amr_values_none(): |
| 208 | + """Test compute_amr_values with None returns None.""" |
| 209 | + assert compute_amr_values(None) is None |
| 210 | + |
| 211 | + |
| 212 | +def test_compute_amr_values_empty_list(): |
| 213 | + """Test compute_amr_values with empty list returns None.""" |
| 214 | + assert compute_amr_values([]) is None |
| 215 | + |
| 216 | + |
| 217 | +def test_compute_amr_values_unknown_method(): |
| 218 | + """Test compute_amr_values with unknown method returns None.""" |
| 219 | + assert compute_amr_values(["unknown_method"]) is None |
| 220 | + |
| 221 | + |
| 222 | +def test_compute_amr_values_mixed_known_unknown(): |
| 223 | + """Test compute_amr_values with mixed known and unknown methods.""" |
| 224 | + result = compute_amr_values(["password", "unknown_method", "otp"]) |
| 225 | + assert set(result) == {"pwd", "otp", "mfa"} |
| 226 | + |
| 227 | + |
| 228 | +def test_compute_amr_values_deduplication(): |
| 229 | + """Test that AMR values are deduplicated (email and sms both give mca).""" |
| 230 | + result = compute_amr_values(["email", "sms"]) |
| 231 | + assert result.count("mca") == 1 |
| 232 | + assert set(result) == {"mca", "sms", "mfa"} |
0 commit comments