1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.security.authentication;
18
19 import java.nio.charset.StandardCharsets;
20 import java.security.MessageDigest;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.Base64;
23 import java.util.Optional;
24 import org.bremersee.exception.ServiceException;
25 import org.springframework.util.MultiValueMap;
26
27
28
29
30
31
32 public interface AccessTokenRetrieverProperties {
33
34
35
36
37
38
39 String getTokenEndpoint();
40
41
42
43
44
45
46 default Optional<BasicAuthProperties> getBasicAuthProperties() {
47 return Optional.empty();
48 }
49
50
51
52
53
54
55 String createCacheKey();
56
57
58
59
60
61
62 default String createCacheKeyHashed() {
63 final String cacheKey = createCacheKey();
64 return Optional.ofNullable(cacheKey)
65 .map(key -> {
66 try {
67 MessageDigest md = MessageDigest.getInstance("SHA-256");
68 byte[] hashBytes = md.digest(key.getBytes(StandardCharsets.UTF_8));
69 return Base64.getEncoder().encodeToString(hashBytes);
70 } catch (NoSuchAlgorithmException e) {
71 throw ServiceException.internalServerError("Creating hash failed.", e);
72 }
73 })
74 .orElse(cacheKey);
75 }
76
77
78
79
80
81
82 MultiValueMap<String, String> createBody();
83
84 }