All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foreach.across.modules.oauth2.services.AuthenticationSerializerObject Maven / Gradle / Ivy

/*
 * Copyright 2014 the original author or authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.foreach.across.modules.oauth2.services;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.OAuth2Request;

import java.io.Serializable;
import java.util.*;

public class AuthenticationSerializerObject implements Serializable
{
	private static final long serialVersionUID = -3165303425428338511L;

	private final String className;
	private final T object;

	// Properties from OAuth2Request
	private final Map requestParameters;
	private final String clientId;
	private final boolean approved;
	private final Set scope;
	private final Set resourceIds;
	private final String redirectUri;
	private final Set responseTypes;
	private final Map extensionProperties;

	public AuthenticationSerializerObject( String className, T object, OAuth2Request oAuth2Request ) {
		this.className = className;
		this.object = object;

		this.requestParameters = oAuth2Request.getRequestParameters();
		this.clientId = oAuth2Request.getClientId();
		this.approved = oAuth2Request.isApproved();
		this.scope = duplicate( oAuth2Request.getScope() );
		this.resourceIds = duplicate( oAuth2Request.getResourceIds() );
		this.redirectUri = oAuth2Request.getRedirectUri();
		this.responseTypes = duplicate( oAuth2Request.getResponseTypes() );
		this.extensionProperties = duplicate( oAuth2Request.getExtensions() );
	}

	private Set duplicate( Set values ) {
		return values != null ? new HashSet<>( values ) : null;
	}

	private Map duplicate( Map values ) {
		return values != null ? new HashMap<>( values ) : null;
	}

	public String getClassName() {
		return className;
	}

	public T getObject() {
		return object;
	}

	public String getClientId() {
		return clientId;
	}

	public OAuth2Request getOAuth2Request( Collection authorities ) {
		return new OAuth2Request( requestParameters,
		                          clientId,
		                          authorities,
		                          approved,
		                          scope,
		                          resourceIds,
		                          redirectUri,
		                          responseTypes,
		                          extensionProperties
		);
	}

	@Override
	public boolean equals( Object o ) {
		if ( this == o ) {
			return true;
		}
		if ( o == null || getClass() != o.getClass() ) {
			return false;
		}
		AuthenticationSerializerObject that = (AuthenticationSerializerObject) o;
		return Objects.equals( approved, that.approved ) &&
				Objects.equals( className, that.className ) &&
				Objects.equals( object, that.object ) &&
				Objects.equals( requestParameters, that.requestParameters ) &&
				Objects.equals( clientId, that.clientId ) &&
				Objects.equals( scope, that.scope ) &&
				Objects.equals( resourceIds, that.resourceIds ) &&
				Objects.equals( redirectUri, that.redirectUri ) &&
				Objects.equals( responseTypes, that.responseTypes ) &&
				Objects.equals( extensionProperties, that.extensionProperties );
	}

	@Override
	public int hashCode() {
		return Objects.hash( className, object, requestParameters, clientId, approved, scope, resourceIds, redirectUri,
		                     responseTypes, extensionProperties );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy