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.core.logic.saml2;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  import java.util.Optional;
24  import java.util.Set;
25  import javax.ws.rs.HttpMethod;
26  import org.apache.syncope.common.lib.saml2.SAML2Constants;
27  import org.apache.syncope.common.lib.saml2.SAML2Response;
28  import org.apache.syncope.common.lib.types.SAML2BindingType;
29  import org.pac4j.core.context.Cookie;
30  import org.pac4j.core.context.WebContext;
31  
32  public class SAML2SP4UIContext implements WebContext {
33  
34      private final String bindingType;
35  
36      private final SAML2Response saml2Response;
37  
38      public SAML2SP4UIContext(final String bindingType, final SAML2Response saml2Response) {
39          this.bindingType = bindingType;
40          this.saml2Response = saml2Response;
41      }
42  
43      @Override
44      public String getRequestMethod() {
45          return SAML2BindingType.POST.getUri().equals(bindingType)
46                  ? HttpMethod.POST
47                  : HttpMethod.GET;
48      }
49  
50      @Override
51      public Optional<String> getRequestParameter(final String name) {
52          switch (name) {
53              case SAML2Constants.SAML_RESPONSE:
54                  return Optional.ofNullable(saml2Response.getSamlResponse());
55  
56              case SAML2Constants.RELAY_STATE:
57                  return Optional.ofNullable(saml2Response.getRelayState());
58  
59              default:
60                  return Optional.empty();
61          }
62      }
63  
64      @Override
65      public Map<String, String[]> getRequestParameters() {
66          return Map.of();
67      }
68  
69      @Override
70      public Optional<String> getRequestAttribute(final String name) {
71          return Optional.empty();
72      }
73  
74      @Override
75      public void setRequestAttribute(final String name, final Object value) {
76          // nothing to do
77      }
78  
79      @Override
80      public Optional<String> getRequestHeader(final String name) {
81          return Optional.empty();
82      }
83  
84      @Override
85      public String getRemoteAddr() {
86          return null;
87      }
88  
89      @Override
90      public void setResponseHeader(final String name, final String value) {
91          // nothing to do
92      }
93  
94      @Override
95      public Optional<String> getResponseHeader(final String s) {
96          return Optional.empty();
97      }
98  
99      @Override
100     public String getServerName() {
101         return null;
102     }
103 
104     @Override
105     public int getServerPort() {
106         return -1;
107     }
108 
109     @Override
110     public String getScheme() {
111         return null;
112     }
113 
114     @Override
115     public boolean isSecure() {
116         return false;
117     }
118 
119     @Override
120     public String getFullRequestURL() {
121         return null;
122     }
123 
124     @Override
125     public Collection<Cookie> getRequestCookies() {
126         return Set.of();
127     }
128 
129     @Override
130     public void addResponseCookie(final Cookie cookie) {
131         // nothing to do
132     }
133 
134     @Override
135     public String getPath() {
136         return null;
137     }
138 
139     @Override
140     public void setResponseContentType(final String content) {
141         // nothing to do
142     }
143 }