001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.data;
019
020import com.unboundid.scim.schema.AttributeDescriptor;
021import com.unboundid.scim.sdk.InvalidResourceException;
022import com.unboundid.scim.sdk.SCIMAttribute;
023import com.unboundid.scim.sdk.SCIMAttributeValue;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * This class represents the AuthenticationSchemes complex attribute in the
030 * Service Provider Config.
031 */
032public class AuthenticationScheme
033{
034  /**
035   * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
036   * to/from <code>AuthenticationScheme</code> instances.
037   */
038  public static final AttributeValueResolver<AuthenticationScheme>
039      AUTHENTICATION_SCHEME_RESOLVER =
040      new AttributeValueResolver<AuthenticationScheme>()
041      {
042        public AuthenticationScheme toInstance(final SCIMAttributeValue value) {
043          Boolean p = value.getSubAttributeValue("primary",
044              BOOLEAN_RESOLVER);
045          return new AuthenticationScheme(
046              value.getSubAttributeValue("name",
047                  STRING_RESOLVER),
048              value.getSubAttributeValue("description",
049                  STRING_RESOLVER),
050              value.getSubAttributeValue("specUrl",
051                  STRING_RESOLVER),
052              value.getSubAttributeValue("documentationUrl",
053                  STRING_RESOLVER),
054              value.getSubAttributeValue("type",
055                  STRING_RESOLVER),
056              p == null ? false : p);
057        }
058
059        @Override
060        public SCIMAttributeValue fromInstance(
061            final AttributeDescriptor addressDescriptor,
062            final AuthenticationScheme value)
063            throws InvalidResourceException {
064          final List<SCIMAttribute> subAttributes =
065              new ArrayList<SCIMAttribute>(8);
066
067          if (value.type != null)
068          {
069            subAttributes.add(
070                SCIMAttribute.create(
071                    addressDescriptor.getSubAttribute("type"),
072                    SCIMAttributeValue.createStringValue(value.type)));
073          }
074
075          if (value.name != null)
076          {
077            subAttributes.add(
078                SCIMAttribute.create(
079                    addressDescriptor.getSubAttribute("name"),
080                    SCIMAttributeValue.createStringValue(value.name)));
081          }
082
083          if (value.description != null)
084          {
085            subAttributes.add(
086                SCIMAttribute.create(
087                    addressDescriptor.getSubAttribute("description"),
088                    SCIMAttributeValue.createStringValue(value.description)));
089          }
090
091          if (value.specUrl != null)
092          {
093            subAttributes.add(
094                SCIMAttribute.create(
095                    addressDescriptor.getSubAttribute("specUrl"),
096                    SCIMAttributeValue.createStringValue(value.specUrl)));
097          }
098
099          if (value.documentationUrl != null)
100          {
101            subAttributes.add(
102                SCIMAttribute.create(
103                    addressDescriptor.getSubAttribute("documentationUrl"),
104                    SCIMAttributeValue.createStringValue(
105                        value.documentationUrl)));
106          }
107
108          if (value.primary)
109          {
110            subAttributes.add(
111                SCIMAttribute.create(
112                    addressDescriptor.getSubAttribute("primary"),
113                    SCIMAttributeValue.createBooleanValue(value.primary)));
114          }
115
116          return SCIMAttributeValue.createComplexValue(subAttributes);
117        }
118      };
119
120
121
122  private String name;
123  private String description;
124  private String specUrl;
125  private String documentationUrl;
126  private String type;
127  private boolean primary;
128
129  /**
130   * Create a value of the SCIM AuthenticationSchemes attribute.
131   *
132   * @param name              The name of the Authentication Scheme.
133   * @param description       The description of the Authentication Scheme.
134   * @param specUrl           A HTTP addressable URL pointing to the
135   *                          Authentication Scheme's specification.
136   * @param documentationUrl  A HTTP addressable URL pointing to the
137   *                          Authentication Scheme's usage documentation.
138   * @param type              The type of Authentication Scheme.
139   * @param primary           Specifies whether this value is the primary value.
140   */
141  public AuthenticationScheme(final String name,
142                              final String description,
143                              final String specUrl,
144                              final String documentationUrl,
145                              final String type,
146                              final boolean primary) {
147    this.name = name;
148    this.description = description;
149    this.specUrl = specUrl;
150    this.documentationUrl = documentationUrl;
151    this.primary = primary;
152    this.type = type;
153  }
154
155  /**
156   * Retrieves the name of the Authentication Scheme.
157   *
158   * @return The name of the Authentication Scheme.
159   */
160  public String getName() {
161    return name;
162  }
163
164  /**
165   * Sets the name of the Authentication Scheme.
166   *
167   * @param name The name of the Authentication Scheme.
168   */
169  public void setName(final String name) {
170    this.name = name;
171  }
172
173  /**
174   * Retrieves the description of the Authentication Scheme.
175   *
176   * @return The description of the Authentication Scheme.
177   */
178  public String getDescription() {
179    return description;
180  }
181
182  /**
183   * Sets the description of the Authentication Scheme.
184   *
185   * @param description The description of the Authentication Scheme.
186   */
187  public void setDescription(final String description) {
188    this.description = description;
189  }
190
191  /**
192   * Retrieves the HTTP addressable URL pointing to the Authentication Scheme's
193   * specification.
194   *
195   * @return The the HTTP addressable URL pointing to the Authentication
196   *         Scheme's specification, or {@code null} if there is none.
197   */
198  public String getSpecUrl() {
199    return specUrl;
200  }
201
202  /**
203   * Sets the HTTP addressable URL pointing to the Authentication Scheme's
204   * specification.
205   * @param specUrl The HTTP addressable URL pointing to the Authentication
206   *                Scheme's specification.
207   */
208  public void setSpecUrl(final String specUrl) {
209    this.specUrl = specUrl;
210  }
211
212  /**
213   * Retrieves the HTTP addressable URL pointing to the Authentication Scheme's
214   * usage documentation.
215   * @return The HTTP addressable URL pointing to the Authentication Scheme's
216   *         usage documentation.
217   */
218  public String getDocumentationUrl() {
219    return documentationUrl;
220  }
221
222  /**
223   * Sets the HTTP addressable URL pointing to the Authentication Scheme's
224   * usage documentation.
225   * @param documentationUrl The HTTP addressable URL pointing to the
226   *                         Authentication Scheme's usage documentation.
227   */
228  public void setDocumentationUrl(final String documentationUrl) {
229    this.documentationUrl = documentationUrl;
230  }
231
232  /**
233   * Indicates whether this value is the primary value.
234   *
235   * @return <code>true</code> if this value is the primary value or
236   * <code>false</code> otherwise.
237   */
238  public boolean isPrimary() {
239    return primary;
240  }
241
242  /**
243   * Specifies whether this value is the primary value.
244   *
245   * @param primary Whether this value is the primary value.
246   */
247  public void setPrimary(final boolean primary) {
248    this.primary = primary;
249  }
250
251  /**
252   * Retrieves the type of Authentication Scheme.
253   *
254   * @return The type of Authentication Scheme.
255   */
256  public String getType() {
257    return type;
258  }
259
260  /**
261   * Sets the type of Authentication Scheme.
262   *
263   * @param type The type of Authentication Scheme.
264   */
265  public void setType(final String type) {
266    this.type = type;
267  }
268
269
270
271  @Override
272  public boolean equals(final Object o)
273  {
274    if (this == o)
275    {
276      return true;
277    }
278    if (o == null || getClass() != o.getClass())
279    {
280      return false;
281    }
282
283    final AuthenticationScheme that = (AuthenticationScheme) o;
284
285    if (primary != that.primary)
286    {
287      return false;
288    }
289    if (description != null ? !description.equals(that.description) :
290        that.description != null)
291    {
292      return false;
293    }
294    if (documentationUrl != null ?
295        !documentationUrl.equals(that.documentationUrl) :
296        that.documentationUrl != null)
297    {
298      return false;
299    }
300    if (name != null ? !name.equals(that.name) : that.name != null)
301    {
302      return false;
303    }
304    if (specUrl != null ? !specUrl.equals(that.specUrl) : that.specUrl != null)
305    {
306      return false;
307    }
308    if (type != null ? !type.equals(that.type) : that.type != null)
309    {
310      return false;
311    }
312
313    return true;
314  }
315
316
317
318  @Override
319  public int hashCode()
320  {
321    int result = name != null ? name.hashCode() : 0;
322    result = 31 * result + (description != null ? description.hashCode() : 0);
323    result = 31 * result + (specUrl != null ? specUrl.hashCode() : 0);
324    result = 31 * result + (documentationUrl != null ?
325                            documentationUrl.hashCode() : 0);
326    result = 31 * result + (type != null ? type.hashCode() : 0);
327    result = 31 * result + (primary ? 1 : 0);
328    return result;
329  }
330
331
332
333  @Override
334  public String toString()
335  {
336    final StringBuilder sb = new StringBuilder();
337    sb.append("AuthenticationScheme");
338    sb.append("{name='").append(name).append('\'');
339    sb.append(", description='").append(description).append('\'');
340    sb.append(", specUrl='").append(specUrl).append('\'');
341    sb.append(", documentationUrl='").append(documentationUrl).append('\'');
342    sb.append(", type='").append(type).append('\'');
343    sb.append(", primary=").append(primary);
344    sb.append('}');
345    return sb.toString();
346  }
347
348
349
350  /**
351   * Convenience method that creates a new AuthenticationScheme instances for
352   * HTTP BASIC.
353   *
354   * @param primary Whether this authentication scheme is primary
355   *
356   * @return A new AuthenticationScheme instances for HTTP BASIC.
357   */
358  public static AuthenticationScheme createBasic(final boolean primary)
359  {
360    return new AuthenticationScheme(
361        "Http Basic",
362        "The HTTP Basic Access Authentication scheme. This scheme is not " +
363            "considered to be a secure method of user authentication (unless " +
364            "used in conjunction with some external secure system such as " +
365            "SSL), as the user name and password are passed over the network " +
366            "as cleartext.",
367        "http://www.ietf.org/rfc/rfc2617.txt",
368        "http://en.wikipedia.org/wiki/Basic_access_authentication",
369        "httpbasic", primary);
370  }
371
372
373
374  /**
375   * Convenience method that creates a new AuthenticationScheme instances for
376   * OAuth 2.
377   *
378   * @param primary Whether this authentication scheme is primary
379   *
380   * @return A new AuthenticationScheme instances for OAuth 2.
381   */
382  public static AuthenticationScheme createOAuth2(final boolean primary)
383  {
384    return new AuthenticationScheme(
385        "OAuth 2.0",
386        "The OAuth 2.0 Bearer Token Authentication scheme. OAuth enables " +
387            "clients to access protected resources by obtaining an access " +
388            "token, which is defined in RFC 6750 as \"a string " +
389            "representing an access authorization issued to the client\", " +
390            "rather than using the resource owner's credentials directly.",
391        "http://tools.ietf.org/html/rfc6750",
392        "http://oauth.net/2/",
393        "oauth2", primary);
394  }
395}