1 package net.sumaris.server.vo.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 }