blob: 5898beba5ce940da0ed52c8a4bdab6e4fe767919 [file] [log] [blame]
margarethaec247dd2018-06-12 21:55:46 +02001package de.ids_mannheim.korap.oauth2.oltu.service;
2
margaretha56fd5582018-06-18 22:14:51 +02003import java.net.URI;
4import java.net.URISyntaxException;
margarethaa2ce63d2018-06-28 10:11:43 +02005import java.time.ZonedDateTime;
margaretha56fd5582018-06-18 22:14:51 +02006
margarethaec247dd2018-06-12 21:55:46 +02007import javax.servlet.http.HttpServletRequest;
8
margarethaffb89502022-04-20 12:03:16 +02009import org.apache.http.HttpStatus;
margarethaec247dd2018-06-12 21:55:46 +020010import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
11import org.apache.oltu.oauth2.as.response.OAuthASResponse;
margarethaffb89502022-04-20 12:03:16 +020012import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
margarethaec247dd2018-06-12 21:55:46 +020013import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
14import org.apache.oltu.oauth2.common.message.OAuthResponse;
15import org.springframework.beans.factory.annotation.Autowired;
16import org.springframework.stereotype.Service;
17
18import com.sun.jersey.api.client.ClientResponse.Status;
19
margaretha33fa3d92018-07-26 13:50:17 +020020import de.ids_mannheim.korap.encryption.RandomCodeGenerator;
margarethaec247dd2018-06-12 21:55:46 +020021import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha56fd5582018-06-18 22:14:51 +020022import de.ids_mannheim.korap.exceptions.StatusCodes;
23import de.ids_mannheim.korap.oauth2.constant.OAuth2Error;
margarethada3c7852018-06-14 20:35:11 +020024import de.ids_mannheim.korap.oauth2.entity.OAuth2Client;
margarethaec247dd2018-06-12 21:55:46 +020025import de.ids_mannheim.korap.oauth2.service.OAuth2AuthorizationService;
margarethaffb89502022-04-20 12:03:16 +020026import de.ids_mannheim.korap.oauth2.service.OAuth2ClientService;
margarethaec247dd2018-06-12 21:55:46 +020027
28/**
29 * OAuth2 authorization service using Apache Oltu
30 *
31 * @author margaretha
32 *
33 */
34@Service
35public class OltuAuthorizationService extends OAuth2AuthorizationService {
36
37 @Autowired
margaretha33fa3d92018-07-26 13:50:17 +020038 private RandomCodeGenerator codeGenerator;
margarethaffb89502022-04-20 12:03:16 +020039 @Autowired
40 private OAuth2ClientService clientService;
margarethaec247dd2018-06-12 21:55:46 +020041
margarethaffb89502022-04-20 12:03:16 +020042 /**e.description("Redirect URI is required");
margarethaec247dd2018-06-12 21:55:46 +020043 * Authorization code request does not require client
44 * authentication, but only checks if the client id exists.
45 *
46 * @param request
47 * @param authzRequest
48 * @param username
margarethaa2ce63d2018-06-28 10:11:43 +020049 * @param authTime
margaretha56fd5582018-06-18 22:14:51 +020050 * @return redirect URI containing authorization code if
51 * successful.
52 *
margarethaec247dd2018-06-12 21:55:46 +020053 * @throws KustvaktException
54 * @throws OAuthSystemException
55 */
56 public String requestAuthorizationCode (HttpServletRequest request,
margarethaa2ce63d2018-06-28 10:11:43 +020057 OAuthAuthzRequest authzRequest, String username,
58 ZonedDateTime authenticationTime)
margaretha56fd5582018-06-18 22:14:51 +020059 throws OAuthSystemException, KustvaktException {
margarethaec247dd2018-06-12 21:55:46 +020060
margarethada3c7852018-06-14 20:35:11 +020061 String clientId = authzRequest.getClientId();
62 OAuth2Client client = clientService.authenticateClientId(clientId);
margaretha56fd5582018-06-18 22:14:51 +020063
margaretha33fa3d92018-07-26 13:50:17 +020064 String redirectUriStr = authzRequest.getRedirectURI();
65 String verifiedRedirectUri = verifyRedirectUri(client, redirectUriStr);
margarethaec247dd2018-06-12 21:55:46 +020066
margaretha56fd5582018-06-18 22:14:51 +020067 URI redirectURI;
68 try {
69 redirectURI = new URI(verifiedRedirectUri);
70 }
71 catch (URISyntaxException e) {
72 throw new KustvaktException(StatusCodes.INVALID_REDIRECT_URI,
73 "Invalid redirect URI", OAuth2Error.INVALID_REQUEST);
74 }
75
margaretha07a356a2018-07-11 19:12:21 +020076 String scope, code;
margaretha56fd5582018-06-18 22:14:51 +020077 try {
margarethaffb89502022-04-20 12:03:16 +020078 //checkResponseType(authzRequest.getResponseType(), redirectURI);
margaretha0666ddb2018-08-02 16:54:04 +020079 code = codeGenerator.createRandomCode();
margaretha56fd5582018-06-18 22:14:51 +020080 scope = createAuthorization(username, authzRequest.getClientId(),
margaretha33fa3d92018-07-26 13:50:17 +020081 redirectUriStr, authzRequest.getScopes(), code,
margarethaa2ce63d2018-06-28 10:11:43 +020082 authenticationTime, null);
margaretha56fd5582018-06-18 22:14:51 +020083 }
84 catch (KustvaktException e) {
85 e.setRedirectUri(redirectURI);
86 throw e;
87 }
88
89 OAuthResponse oAuthResponse;
90 try {
91 oAuthResponse = OAuthASResponse
92 .authorizationResponse(request,
93 Status.FOUND.getStatusCode())
margarethaffb89502022-04-20 12:03:16 +020094 .setCode(code).setScope(scope)
95 .location(verifiedRedirectUri)
margaretha56fd5582018-06-18 22:14:51 +020096 .buildQueryMessage();
97 }
98 catch (OAuthSystemException e) {
99 // Should not happen
100 KustvaktException ke =
101 new KustvaktException(StatusCodes.OAUTH2_SYSTEM_ERROR,
102 e.getMessage(), OAuth2Error.SERVER_ERROR);
103 ke.setRedirectUri(redirectURI);
104 throw ke;
105 }
margarethaec247dd2018-06-12 21:55:46 +0200106 return oAuthResponse.getLocationUri();
107 }
margarethaffb89502022-04-20 12:03:16 +0200108
109 public OAuthProblemException checkRedirectUri (OAuthProblemException e,
110 String clientId, String redirectUri) {
margarethaef1883f2022-05-25 12:24:12 +0200111 if (clientId !=null && !clientId.isEmpty()) {
margarethaffb89502022-04-20 12:03:16 +0200112 String registeredUri = null;
113 try {
114 OAuth2Client client = clientService.retrieveClient(clientId);
115 registeredUri = client.getRedirectURI();
116 }
117 catch (KustvaktException e1) {}
118
119 if (redirectUri != null && !redirectUri.isEmpty()) {
120 if (registeredUri != null && !registeredUri.isEmpty()
121 && !redirectUri.equals(registeredUri)) {
122 e.description("Invalid redirect URI");
123 }
124 else {
125 e.setRedirectUri(redirectUri);
126 e.responseStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
127 }
128 }
129 else if (registeredUri != null && !registeredUri.isEmpty()) {
130 e.setRedirectUri(registeredUri);
131 e.responseStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
132 }
133 else {
134 e.description("Redirect URI is required");
135 }
136 }
137
138 return e;
139 }
140
141 public KustvaktException checkRedirectUri (KustvaktException e,
142 String clientId, String redirectUri){
143 int statusCode = e.getStatusCode();
144 if (!clientId.isEmpty()
145 && statusCode != StatusCodes.CLIENT_NOT_FOUND
margaretha9436ebe2022-04-22 11:48:37 +0200146 && statusCode != StatusCodes.AUTHORIZATION_FAILED
147 && statusCode != StatusCodes.INVALID_REDIRECT_URI) {
margarethaffb89502022-04-20 12:03:16 +0200148 String registeredUri = null;
149 try {
150 OAuth2Client client = clientService.retrieveClient(clientId);
151 registeredUri = client.getRedirectURI();
152 }
153 catch (KustvaktException e1) {}
154
155 if (redirectUri != null && !redirectUri.isEmpty()) {
156 if (registeredUri != null && !registeredUri.isEmpty()
157 && !redirectUri.equals(registeredUri)) {
158 return new KustvaktException(StatusCodes.INVALID_REDIRECT_URI,
159 "Invalid redirect URI", OAuth2Error.INVALID_REQUEST);
160 }
161 else {
162 try {
163 e.setRedirectUri(new URI(redirectUri));
164 }
165 catch (URISyntaxException e1) {
166 return new KustvaktException(StatusCodes.INVALID_REDIRECT_URI,
167 "Invalid redirect URI", OAuth2Error.INVALID_REQUEST);
168 }
169 e.setResponseStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
170 }
171 }
172 else if (registeredUri != null && !registeredUri.isEmpty()) {
173 try {
174 e.setRedirectUri(new URI(registeredUri));
175 }
176 catch (URISyntaxException e1) {
177 return new KustvaktException(StatusCodes.INVALID_REDIRECT_URI,
178 "Invalid redirect URI", OAuth2Error.INVALID_REQUEST);
179 }
180 e.setResponseStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
181 }
182 else {
183 return new KustvaktException(StatusCodes.MISSING_REDIRECT_URI,
184 "Redirect URI is required", OAuth2Error.INVALID_REQUEST);
185 }
186 }
187
188 return e;
189 }
margarethaec247dd2018-06-12 21:55:46 +0200190}