View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons.resources.oidcc4ui;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.ws.rs.core.HttpHeaders;
23  import javax.ws.rs.core.Response;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.ui.commons.BaseSession;
26  import org.apache.syncope.client.ui.commons.annotations.Resource;
27  import org.apache.syncope.client.ui.commons.panels.OIDCC4UIConstants;
28  import org.apache.syncope.common.lib.oidc.OIDCConstants;
29  import org.apache.syncope.common.lib.oidc.OIDCRequest;
30  import org.apache.syncope.common.rest.api.service.OIDCC4UIService;
31  import org.apache.wicket.Session;
32  import org.apache.wicket.request.resource.AbstractResource;
33  
34  @Resource(
35          key = OIDCC4UIConstants.URL_CONTEXT + ".login",
36          path = "/" + OIDCC4UIConstants.URL_CONTEXT + "/login")
37  public class LoginResource extends AbstractResource {
38  
39      private static final long serialVersionUID = -3076690953674174306L;
40  
41      @Override
42      protected ResourceResponse newResourceResponse(final Attributes attributes) {
43          String op = attributes.getRequest().getQueryParameters().
44                  getParameterValue(OIDCC4UIConstants.PARAM_OP).toString();
45  
46          HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
47          String redirectURI = StringUtils.substringBefore(
48                  request.getRequestURL().toString(), "/login") + "/code-consumer";
49  
50          OIDCC4UIService service = BaseSession.class.cast(Session.get()).getAnonymousService(OIDCC4UIService.class);
51          OIDCRequest loginRequest = service.createLoginRequest(redirectURI, op);
52  
53          Session.get().setAttribute(OIDCConstants.OP, op);
54  
55          ResourceResponse response = new ResourceResponse();
56          response.setStatusCode(Response.Status.FOUND.getStatusCode());
57          response.getHeaders().addHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
58          response.getHeaders().addHeader("Pragma", "no-cache");
59          response.getHeaders().addHeader(HttpHeaders.LOCATION, loginRequest.getLocation());
60  
61          return response;
62      }
63  }