View Javadoc
1   package net.sumaris.server.vo.security;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import lombok.Data;
26  
27  import java.text.ParseException;
28  
29  @Data
30  public class AuthDataVO {
31      private String pubkey;
32      private String challenge;
33      private String signature;
34  
35      public AuthDataVO(){
36      }
37  
38      public AuthDataVO(String pubkey, String challenge, String signature) {
39          this.pubkey = pubkey;
40          this.challenge = challenge;
41          this.signature = signature;
42      }
43  
44      public String toString() {
45          return String.format("%s:%s|%s", pubkey, challenge, signature);
46      }
47  
48      public String asToken() {
49          return toString();
50      }
51  
52      public static AuthDataVO parse(String token) throws ParseException {
53          int index1 = token.indexOf(':');
54          if (index1 == -1) {
55              throw new ParseException("Invalid token. Expected format is: <pubkey>:<challenge>|<signature>", 0);
56          }
57          int index2 = token.indexOf('|', index1);
58          if (index2 == -1) {
59              throw new ParseException("Invalid token. Expected format is: <pubkey>:<challenge>|<signature>", index1);
60          }
61          return new AuthDataVO(
62                  token.substring(0, index1),
63                  token.substring(index1+1, index2),
64                  token.substring(index2+1));
65      }
66  
67  }