|
9 | 9 | */
|
10 | 10 | package org.zowe.apiml.gateway.security.service.saf;
|
11 | 11 |
|
12 |
| -import com.netflix.zuul.context.RequestContext; |
13 |
| -import lombok.Data; |
14 |
| -import lombok.RequiredArgsConstructor; |
15 |
| -import lombok.extern.slf4j.Slf4j; |
| 12 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 13 | +import com.fasterxml.jackson.databind.ser.std.StdArraySerializers; |
| 14 | +import lombok.*; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
16 | 16 | import org.springframework.beans.factory.annotation.Value;
|
17 |
| -import org.springframework.http.ResponseEntity; |
| 17 | +import org.springframework.http.*; |
18 | 18 | import org.springframework.web.client.HttpClientErrorException;
|
| 19 | +import org.springframework.web.client.RestClientException; |
19 | 20 | import org.springframework.web.client.RestTemplate;
|
20 |
| -import org.zowe.apiml.gateway.security.service.AuthenticationService; |
21 |
| -import org.zowe.apiml.passticket.IRRPassTicketGenerationException; |
22 |
| -import org.zowe.apiml.passticket.PassTicketService; |
23 |
| -import org.zowe.apiml.security.common.token.TokenAuthentication; |
24 | 21 |
|
25 | 22 | import java.net.URI;
|
26 |
| -import java.util.Optional; |
| 23 | +import java.util.Collections; |
27 | 24 |
|
28 | 25 | import static org.springframework.util.StringUtils.isEmpty;
|
29 | 26 |
|
|
37 | 34 | * - apiml.security.saf.urls.verify - URL to verify the validity of the token
|
38 | 35 | */
|
39 | 36 | @RequiredArgsConstructor
|
40 |
| -@Slf4j |
41 | 37 | public class SafRestAuthenticationService implements SafIdtProvider {
|
| 38 | + |
42 | 39 | private final RestTemplate restTemplate;
|
43 |
| - private final AuthenticationService authenticationService; |
44 |
| - private final PassTicketService passTicketService; |
| 40 | + |
| 41 | + static final HttpHeaders HEADERS = new HttpHeaders(); |
| 42 | + |
| 43 | + static { |
| 44 | + HEADERS.setContentType(MediaType.APPLICATION_JSON); |
| 45 | + HEADERS.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); |
| 46 | + } |
45 | 47 |
|
46 | 48 | @Value("${apiml.security.saf.urls.authenticate}")
|
47 | 49 | String authenticationUrl;
|
48 | 50 | @Value("${apiml.security.saf.urls.verify}")
|
49 | 51 | String verifyUrl;
|
50 |
| - @Value("${apiml.security.zosmf.applid:IZUDFLT}") |
51 |
| - protected String zosmfApplId; |
52 | 52 |
|
53 | 53 | @Override
|
54 | 54 | public String generate(String username, char[] password, String applId) {
|
55 |
| - final RequestContext context = RequestContext.getCurrentContext(); |
56 |
| - Optional<String> jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()); |
57 |
| - if (!jwtToken.isPresent()) { |
58 |
| - throw new SafIdtException("Provided no JWT token"); |
59 |
| - } |
60 |
| - |
61 |
| - TokenAuthentication tokenAuthentication = authenticationService.validateJwtToken(jwtToken.get()); |
62 |
| - if (!tokenAuthentication.isAuthenticated()) { |
63 |
| - throw new SafIdtException("Provided invalid JWT token"); |
64 |
| - } |
| 55 | + Authentication authentication = Authentication.builder() |
| 56 | + .username(username) |
| 57 | + .pass(password) |
| 58 | + .appl(applId) |
| 59 | + .build(); |
65 | 60 |
|
66 | 61 | try {
|
67 |
| - Authentication authentication = new Authentication(); |
68 |
| - authentication.setJwt(jwtToken.get()); |
69 |
| - authentication.setUsername(username); |
70 |
| - String passTicket = passTicketService.generate(username, zosmfApplId); |
71 |
| - log.debug("Generated passticket: {}", passTicket); |
72 |
| - authentication.setPass(passTicket); |
73 |
| - |
74 |
| - ResponseEntity<Token> re = restTemplate.postForEntity(URI.create(authenticationUrl), authentication, Token.class); |
75 |
| - |
76 |
| - if (!re.getStatusCode().is2xxSuccessful()) { |
77 |
| - throw new SafIdtException("ZSS authentication service has not returned the Identity token"); |
78 |
| - } |
79 |
| - |
80 |
| - Token responseBody = re.getBody(); |
81 |
| - if (responseBody == null) { |
| 62 | + ResponseEntity<Token> response = restTemplate.exchange( |
| 63 | + URI.create(authenticationUrl), |
| 64 | + HttpMethod.POST, |
| 65 | + new HttpEntity<>(authentication, HEADERS), |
| 66 | + Token.class); |
| 67 | + |
| 68 | + Token responseBody = response.getBody(); |
| 69 | + if (responseBody == null || StringUtils.isEmpty(responseBody.getJwt())) { |
82 | 70 | throw new SafIdtException("ZSS authentication service has not returned the Identity token");
|
83 | 71 | }
|
84 | 72 |
|
85 | 73 | return responseBody.getJwt();
|
86 |
| - } catch (HttpClientErrorException.Unauthorized | HttpClientErrorException.Forbidden | IRRPassTicketGenerationException e) { |
| 74 | + } catch (HttpClientErrorException.Unauthorized | HttpClientErrorException.Forbidden e) { |
87 | 75 | throw new SafIdtAuthException("Authentication to ZSS failed", e);
|
88 | 76 | }
|
89 | 77 | }
|
90 | 78 |
|
91 | 79 | @Override
|
92 |
| - public boolean verify(String safToken, String applId) { |
| 80 | + public boolean verify(String safToken, String applid) { |
93 | 81 | if (isEmpty(safToken)) {
|
94 | 82 | return false;
|
95 | 83 | }
|
96 | 84 |
|
97 | 85 | try {
|
98 |
| - Token token = new Token(); |
99 |
| - token.setJwt(safToken); |
100 |
| - |
101 |
| - ResponseEntity<String> re = restTemplate.postForEntity(URI.create(verifyUrl), token, String.class); |
102 |
| - |
103 |
| - return re.getStatusCode().is2xxSuccessful(); |
104 |
| - } catch (HttpClientErrorException.Unauthorized e) { |
| 86 | + ResponseEntity<Void> response = restTemplate.exchange( |
| 87 | + URI.create(verifyUrl), |
| 88 | + HttpMethod.POST, |
| 89 | + new HttpEntity<>(new Token(safToken, applid), HEADERS), |
| 90 | + Void.class); |
| 91 | + |
| 92 | + return response.getStatusCode().is2xxSuccessful(); |
| 93 | + } catch (RestClientException e) { |
105 | 94 | return false;
|
106 | 95 | }
|
107 | 96 | }
|
108 | 97 |
|
109 | 98 | @Data
|
| 99 | + @NoArgsConstructor |
| 100 | + @AllArgsConstructor |
110 | 101 | public static class Token {
|
111 | 102 | String jwt;
|
| 103 | + String appl; |
112 | 104 | }
|
113 | 105 |
|
114 |
| - @Data |
| 106 | + @lombok.Value |
| 107 | + @Builder |
115 | 108 | public static class Authentication {
|
116 |
| - String jwt; |
117 | 109 | String username;
|
118 |
| - String pass; |
| 110 | + @JsonSerialize(using = StdArraySerializers.CharArraySerializer.class) |
| 111 | + char[] pass; |
| 112 | + String appl; |
119 | 113 | }
|
| 114 | + |
120 | 115 | }
|
0 commit comments