-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathWebAuthnCredentials.java
More file actions
135 lines (111 loc) · 3.75 KB
/
WebAuthnCredentials.java
File metadata and controls
135 lines (111 loc) · 3.75 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2019 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.auth.webauthn;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.authentication.CredentialValidationException;
import io.vertx.ext.auth.authentication.Credentials;
@DataObject(generateConverter = true)
public class WebAuthnCredentials implements Credentials {
private String challenge;
private JsonObject webauthn;
private String username;
private String userId;
private String origin;
private String domain;
public WebAuthnCredentials() {}
public WebAuthnCredentials(JsonObject json) {
WebAuthnCredentialsConverter.fromJson(json, this);
}
public String getChallenge() {
return challenge;
}
public WebAuthnCredentials setChallenge(String challenge) {
this.challenge = challenge;
return this;
}
public JsonObject getWebauthn() {
return webauthn;
}
public WebAuthnCredentials setWebauthn(JsonObject webauthn) {
this.webauthn = webauthn;
return this;
}
public String getUsername() {
return username;
}
public WebAuthnCredentials setUsername(String username) {
this.username = username;
return this;
}
public String getOrigin() {
return origin;
}
public WebAuthnCredentials setOrigin(String origin) {
this.origin = origin;
return this;
}
public String getDomain() {
return domain;
}
public WebAuthnCredentials setDomain(String domain) {
this.domain = domain;
return this;
}
public String getUserId() {
return userId;
}
public WebAuthnCredentials setUserId(String userId) {
this.userId = userId;
return this;
}
@Override
public <V> void checkValid(V arg) throws CredentialValidationException {
if (challenge == null || challenge.length() == 0) {
throw new CredentialValidationException("Challenge cannot be null or empty");
}
if (webauthn == null) {
throw new CredentialValidationException("webauthn cannot be null");
}
if (!webauthn.containsKey("id") || !webauthn.containsKey("rawId") || !webauthn.containsKey("response")) {
throw new CredentialValidationException("Invalid webauthn JSON, missing one of {id, rawId, response}");
}
if (!webauthn.getString("id").equals(webauthn.getString("rawId"))) {
throw new CredentialValidationException("Invalid webauthn {id} not base64url encoded");
}
try {
JsonObject response = webauthn.getJsonObject("response");
// response.clientDataJSON must be always present
if (!response.containsKey("clientDataJSON")) {
throw new CredentialValidationException("Missing webauthn.response.clientDataJSON");
}
// if response.userHandle is present it should be a String
if (response.containsKey("userHandle")) {
if (!(response.getValue("userHandle") instanceof String)) {
throw new CredentialValidationException("webauthn.response.userHandle must be String");
}
}
} catch (ClassCastException e) {
throw new CredentialValidationException("webauthn.response must be JSON");
}
// Username may be null once the system has stored it once.
}
public JsonObject toJson() {
final JsonObject json = new JsonObject();
WebAuthnCredentialsConverter.toJson(this, json);
return json;
}
}