View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core.wa;
20  
21  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.UUID;
27  import org.apache.syncope.common.lib.wa.WebAuthnAccount;
28  import org.apache.syncope.common.lib.wa.WebAuthnDeviceCredential;
29  import org.apache.syncope.core.spring.security.SecureRandomUtils;
30  import org.apache.syncope.fit.AbstractITCase;
31  import org.junit.jupiter.api.Test;
32  
33  public class WebAuthnAccountITCase extends AbstractITCase {
34  
35      private static WebAuthnAccount createWebAuthnRegisteredAccount() {
36          String id = SecureRandomUtils.generateRandomUUID().toString();
37          String record = "[ {"
38                  + "    \"userIdentity\" : {"
39                  + "      \"name\" : \"%s\","
40                  + "      \"displayName\" : \"%s\""
41                  + "    },"
42                  + "    \"credential\" : {"
43                  + "      \"credentialId\" : \"fFGyV3K5x1\""
44                  + "    },"
45                  + "    \"username\" : \"%s\""
46                  + "  } ]";
47          WebAuthnDeviceCredential credential = new WebAuthnDeviceCredential.Builder().
48                  json(String.format(record, id, id, id)).
49                  identifier("fFGyV3K5x1").
50                  build();
51          return new WebAuthnAccount.Builder().credential(credential).build();
52      }
53  
54      @Test
55      public void listAndFind() {
56          String owner = UUID.randomUUID().toString();
57          WebAuthnAccount acct = createWebAuthnRegisteredAccount();
58          WEBAUTHN_REGISTRATION_SERVICE.create(owner, acct);
59          assertFalse(WEBAUTHN_REGISTRATION_SERVICE.list().isEmpty());
60          assertNotNull(WEBAUTHN_REGISTRATION_SERVICE.read(owner));
61      }
62  
63      @Test
64      public void deleteByOwner() {
65          String owner = UUID.randomUUID().toString();
66          WebAuthnAccount acct = createWebAuthnRegisteredAccount();
67          WEBAUTHN_REGISTRATION_SERVICE.create(owner, acct);
68          WEBAUTHN_REGISTRATION_SERVICE.delete(owner);
69          assertTrue(WEBAUTHN_REGISTRATION_SERVICE.read(owner).getCredentials().isEmpty());
70      }
71  
72      @Test
73      public void deleteByAcct() {
74          String owner = UUID.randomUUID().toString();
75          WebAuthnAccount acct = createWebAuthnRegisteredAccount();
76          WEBAUTHN_REGISTRATION_SERVICE.create(owner, acct);
77          WEBAUTHN_REGISTRATION_SERVICE.delete(owner, acct.getCredentials().get(0).getIdentifier());
78          acct = WEBAUTHN_REGISTRATION_SERVICE.read(owner);
79          assertTrue(acct.getCredentials().isEmpty());
80      }
81  
82      @Test
83      public void create() {
84          WebAuthnAccount acct = createWebAuthnRegisteredAccount();
85          assertDoesNotThrow(() -> WEBAUTHN_REGISTRATION_SERVICE.create(UUID.randomUUID().toString(), acct));
86      }
87  }