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 com.fasterxml.jackson.annotation.JsonInclude;
22  import com.fasterxml.jackson.core.JsonProcessingException;
23  import com.fasterxml.jackson.databind.json.JsonMapper;
24  import javax.servlet.http.HttpServletRequest;
25  import org.apache.commons.lang3.tuple.Pair;
26  import org.apache.syncope.client.ui.commons.BaseSession;
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.OIDCLoginResponse;
30  import org.apache.syncope.common.lib.to.UserTO;
31  import org.apache.syncope.common.rest.api.service.OIDCC4UIService;
32  import org.apache.wicket.RestartResponseException;
33  import org.apache.wicket.Session;
34  import org.apache.wicket.WicketRuntimeException;
35  import org.apache.wicket.markup.html.WebPage;
36  import org.apache.wicket.request.mapper.parameter.PageParameters;
37  import org.apache.wicket.request.resource.AbstractResource;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  public abstract class CodeConsumerResource extends AbstractResource {
42  
43      private static final long serialVersionUID = -692581789294259519L;
44  
45      protected static final Logger LOG = LoggerFactory.getLogger(CodeConsumerResource.class);
46  
47      protected static final JsonMapper MAPPER =
48              JsonMapper.builder().findAndAddModules().serializationInclusion(JsonInclude.Include.NON_EMPTY).build();
49  
50      protected abstract Class<? extends WebPage> getLoginPageClass();
51  
52      protected abstract Pair<Class<? extends WebPage>, PageParameters> getSelfRegInfo(UserTO newUser)
53              throws JsonProcessingException;
54  
55      @Override
56      protected ResourceResponse newResourceResponse(final Attributes attributes) {
57          String authorizationCode = attributes.getRequest().getQueryParameters().
58                  getParameterValue(OIDCConstants.CODE).toOptionalString();
59  
60          HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
61  
62          OIDCC4UIService service = BaseSession.class.cast(Session.get()).getAnonymousService(OIDCC4UIService.class);
63          OIDCLoginResponse oidcResponse = service.login(
64                  request.getRequestURL().toString(),
65                  authorizationCode,
66                  Session.get().getAttribute(OIDCConstants.OP).toString());
67  
68          if (oidcResponse.isSelfReg()) {
69              UserTO newUser = new UserTO();
70              newUser.setUsername(oidcResponse.getUsername());
71              newUser.getPlainAttrs().addAll(oidcResponse.getAttrs());
72  
73              try {
74                  Pair<Class<? extends WebPage>, PageParameters> selfRegInfo = getSelfRegInfo(newUser);
75                  throw new RestartResponseException(selfRegInfo.getLeft(), selfRegInfo.getRight());
76              } catch (JsonProcessingException e) {
77                  LOG.error("Could not serialize new user {}", newUser, e);
78                  throw new WicketRuntimeException(e);
79              }
80          } else {
81              throw new RestartResponseException(
82                      getLoginPageClass(),
83                      new PageParameters().
84                              set(OIDCC4UIConstants.OIDCC4UI_JWT, oidcResponse.getAccessToken()).
85                              set(OIDCC4UIConstants.OIDCC4UI_SLO_SUPPORTED, oidcResponse.isLogoutSupported()));
86          }
87      }
88  }