2019-10-01 13:17:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2019 the original author or authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.keycloak.credential;
|
|
|
|
|
2022-02-03 13:39:51 +00:00
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2019-10-01 13:17:38 +00:00
|
|
|
import org.keycloak.common.util.Base64;
|
|
|
|
|
2020-02-19 00:05:58 +00:00
|
|
|
import com.webauthn4j.data.AuthenticationParameters;
|
|
|
|
import com.webauthn4j.data.AuthenticationRequest;
|
2022-02-03 13:39:51 +00:00
|
|
|
import com.webauthn4j.data.AuthenticatorTransport;
|
2019-10-01 13:17:38 +00:00
|
|
|
import com.webauthn4j.data.attestation.authenticator.AttestedCredentialData;
|
2019-11-05 08:23:09 +00:00
|
|
|
import com.webauthn4j.data.attestation.authenticator.COSEKey;
|
2019-10-01 13:17:38 +00:00
|
|
|
import com.webauthn4j.data.attestation.statement.AttestationStatement;
|
|
|
|
|
2022-02-07 08:43:28 +00:00
|
|
|
import java.util.Collections;
|
2022-02-03 13:39:51 +00:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2019-11-14 13:45:05 +00:00
|
|
|
public class WebAuthnCredentialModelInput implements CredentialInput {
|
|
|
|
|
2019-10-01 13:17:38 +00:00
|
|
|
private AttestedCredentialData attestedCredentialData;
|
|
|
|
private AttestationStatement attestationStatement;
|
2020-02-19 00:05:58 +00:00
|
|
|
private AuthenticationParameters authenticationParameters; // not persisted because it can only be used on authentication operation.
|
|
|
|
private AuthenticationRequest authenticationRequest; // not persisted because it can only be used on authentication operation.
|
2019-10-01 13:17:38 +00:00
|
|
|
private long count;
|
2019-11-14 13:45:05 +00:00
|
|
|
private String credentialDBId;
|
2020-01-29 08:33:45 +00:00
|
|
|
private final String credentialType;
|
2020-02-25 03:40:50 +00:00
|
|
|
private String attestationStatementFormat;
|
2022-02-03 13:39:51 +00:00
|
|
|
private Set<AuthenticatorTransport> transports;
|
2020-01-29 08:33:45 +00:00
|
|
|
|
|
|
|
public WebAuthnCredentialModelInput(String credentialType) {
|
|
|
|
this.credentialType = credentialType;
|
|
|
|
}
|
2019-11-14 13:45:05 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getCredentialId() {
|
|
|
|
return credentialDBId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getChallengeResponse() {
|
|
|
|
throw new UnsupportedOperationException("WebAuthn credential doesn't support getChallengeResponse");
|
|
|
|
}
|
2019-10-01 13:17:38 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getType() {
|
2020-01-29 08:33:45 +00:00
|
|
|
return credentialType;
|
2019-10-01 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public AttestedCredentialData getAttestedCredentialData() {
|
|
|
|
return attestedCredentialData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AttestationStatement getAttestationStatement() {
|
|
|
|
return attestationStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getCount() {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-02-19 00:05:58 +00:00
|
|
|
public AuthenticationParameters getAuthenticationParameters() {
|
|
|
|
return authenticationParameters;
|
2019-10-01 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 00:05:58 +00:00
|
|
|
public void setAuthenticationParameters(AuthenticationParameters authenticationParameters) {
|
|
|
|
this.authenticationParameters = authenticationParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AuthenticationRequest getAuthenticationRequest() {
|
|
|
|
return authenticationRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAuthenticationRequest(AuthenticationRequest authenticationRequest) {
|
|
|
|
this.authenticationRequest = authenticationRequest;
|
2019-10-01 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttestedCredentialData(AttestedCredentialData attestedCredentialData) {
|
|
|
|
this.attestedCredentialData = attestedCredentialData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttestationStatement(AttestationStatement attestationStatement) {
|
|
|
|
this.attestationStatement = attestationStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCount(long count) {
|
|
|
|
this.count = count;
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:45:05 +00:00
|
|
|
public String getCredentialDBId() {
|
|
|
|
return credentialDBId;
|
2019-10-01 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:45:05 +00:00
|
|
|
public void setCredentialDBId(String credentialDBId) {
|
|
|
|
this.credentialDBId = credentialDBId;
|
2019-10-01 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 08:33:45 +00:00
|
|
|
public String getCredentialType() {
|
|
|
|
return credentialType;
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:40:50 +00:00
|
|
|
public String getAttestationStatementFormat() {
|
|
|
|
return attestationStatementFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttestationStatementFormat(String attestationStatementFormat) {
|
|
|
|
this.attestationStatementFormat = attestationStatementFormat;
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:39:51 +00:00
|
|
|
public Set<AuthenticatorTransport> getTransports() {
|
2022-02-07 08:43:28 +00:00
|
|
|
return transports != null ? transports : Collections.emptySet();
|
2022-02-03 13:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransports(Set<AuthenticatorTransport> transports) {
|
|
|
|
this.transports = transports;
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:17:38 +00:00
|
|
|
public String toString() {
|
2020-01-29 08:33:45 +00:00
|
|
|
StringBuilder sb = new StringBuilder("Credential Type = " + credentialType + ",");
|
2019-11-14 13:45:05 +00:00
|
|
|
if (credentialDBId != null)
|
|
|
|
sb.append("Credential DB Id = ")
|
|
|
|
.append(credentialDBId)
|
2019-10-01 13:17:38 +00:00
|
|
|
.append(",");
|
2020-02-25 03:40:50 +00:00
|
|
|
if (attestationStatement != null) {
|
2019-10-01 13:17:38 +00:00
|
|
|
sb.append("Attestation Statement Format = ")
|
|
|
|
.append(attestationStatement.getFormat())
|
|
|
|
.append(",");
|
2020-02-25 03:40:50 +00:00
|
|
|
} else if (attestationStatementFormat != null) {
|
|
|
|
sb.append("Attestation Statement Format = ")
|
|
|
|
.append(attestationStatementFormat)
|
|
|
|
.append(",");
|
|
|
|
}
|
2019-10-01 13:17:38 +00:00
|
|
|
if (attestedCredentialData != null) {
|
|
|
|
sb.append("AAGUID = ")
|
|
|
|
.append(attestedCredentialData.getAaguid().toString())
|
|
|
|
.append(",");
|
|
|
|
sb.append("CREDENTIAL_ID = ")
|
|
|
|
.append(Base64.encodeBytes(attestedCredentialData.getCredentialId()))
|
|
|
|
.append(",");
|
2019-11-05 08:23:09 +00:00
|
|
|
COSEKey credPubKey = attestedCredentialData.getCOSEKey();
|
2019-10-01 13:17:38 +00:00
|
|
|
byte[] keyId = credPubKey.getKeyId();
|
|
|
|
if (keyId != null)
|
|
|
|
sb.append("CREDENTIAL_PUBLIC_KEY.key_id = ")
|
|
|
|
.append(Base64.encodeBytes(keyId))
|
|
|
|
.append(",");
|
|
|
|
sb.append("CREDENTIAL_PUBLIC_KEY.algorithm = ")
|
2019-11-05 08:23:09 +00:00
|
|
|
.append(String.valueOf(credPubKey.getAlgorithm().getValue()))
|
2019-10-01 13:17:38 +00:00
|
|
|
.append(",");
|
|
|
|
sb.append("CREDENTIAL_PUBLIC_KEY.key_type = ")
|
|
|
|
.append(credPubKey.getKeyType().name())
|
|
|
|
.append(",");
|
|
|
|
}
|
2020-02-19 00:05:58 +00:00
|
|
|
if (authenticationRequest != null) {
|
2019-10-01 13:17:38 +00:00
|
|
|
// only set on Authentication
|
|
|
|
sb.append("Credential Id = ")
|
2020-02-19 00:05:58 +00:00
|
|
|
.append(Base64.encodeBytes(authenticationRequest.getCredentialId()))
|
2019-10-01 13:17:38 +00:00
|
|
|
.append(",");
|
|
|
|
}
|
2022-02-07 08:43:28 +00:00
|
|
|
if (CollectionUtils.isNotEmpty(getTransports())) {
|
|
|
|
final String transportsString = getTransports().stream()
|
2022-02-03 13:39:51 +00:00
|
|
|
.map(AuthenticatorTransport::getValue)
|
|
|
|
.collect(Collectors.joining(","));
|
|
|
|
|
|
|
|
sb.append("Transports = [")
|
|
|
|
.append(transportsString)
|
|
|
|
.append("],");
|
|
|
|
}
|
2019-10-01 13:17:38 +00:00
|
|
|
if (sb.length() > 0)
|
|
|
|
sb.deleteCharAt(sb.lastIndexOf(","));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|