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.wa.starter.gauth;
20  
21  import java.time.LocalDateTime;
22  import org.apache.syncope.common.lib.wa.GoogleMfaAuthToken;
23  import org.apache.syncope.common.rest.api.service.wa.GoogleMfaAuthTokenService;
24  import org.apache.syncope.wa.bootstrap.WARestClient;
25  import org.apereo.cas.gauth.token.GoogleAuthenticatorToken;
26  import org.apereo.cas.otp.repository.token.BaseOneTimeTokenRepository;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  public class WAGoogleMfaAuthTokenRepository extends BaseOneTimeTokenRepository<GoogleAuthenticatorToken> {
31  
32      protected static final Logger LOG = LoggerFactory.getLogger(WAGoogleMfaAuthTokenRepository.class);
33  
34      protected final WARestClient waRestClient;
35  
36      protected final long expireTokensInSeconds;
37  
38      public WAGoogleMfaAuthTokenRepository(final WARestClient waRestClient, final long expireTokensInSeconds) {
39          this.waRestClient = waRestClient;
40          this.expireTokensInSeconds = expireTokensInSeconds;
41      }
42  
43      protected GoogleMfaAuthTokenService service() {
44          return waRestClient.getService(GoogleMfaAuthTokenService.class);
45      }
46  
47      @Override
48      protected void cleanInternal() {
49          service().delete(LocalDateTime.now().minusSeconds(expireTokensInSeconds));
50      }
51  
52      @Override
53      public void store(final GoogleAuthenticatorToken token) {
54          GoogleMfaAuthToken tokenTO = new GoogleMfaAuthToken.Builder().
55                  token(token.getToken()).
56                  issueDate(token.getIssuedDateTime()).
57                  build();
58          service().store(token.getUserId(), tokenTO);
59      }
60  
61      @Override
62      public GoogleAuthenticatorToken get(final String username, final Integer otp) {
63          try {
64              GoogleMfaAuthToken tokenTO = service().read(username, otp);
65              GoogleAuthenticatorToken token = new GoogleAuthenticatorToken(tokenTO.getOtp(), username);
66              token.setIssuedDateTime(tokenTO.getIssueDate());
67              return token;
68          } catch (final Exception e) {
69              LOG.debug("Unable to fetch token {} for user {}", otp, username);
70          }
71          return null;
72      }
73  
74      @Override
75      public void remove(final String username, final Integer otp) {
76          service().delete(username, otp);
77      }
78  
79      @Override
80      public void remove(final String username) {
81          service().delete(username);
82      }
83  
84      @Override
85      public void remove(final Integer otp) {
86          service().delete(otp);
87      }
88  
89      @Override
90      public void removeAll() {
91          service().delete((LocalDateTime) null);
92      }
93  
94      @Override
95      public long count(final String username) {
96          return service().read(username).getTotalCount();
97      }
98  
99      @Override
100     public long count() {
101         return service().list().getTotalCount();
102     }
103 }