org.ctoolkit.restapi.client.identity.GoogleApiIdentityToolkitModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ctoolkit-client-identity-toolkit Show documentation
Show all versions of ctoolkit-client-identity-toolkit Show documentation
Google API CtoolkiT REST API Client Implementation of Google Identity Toolkit
/*
* Copyright (c) 2016 Comvai, s.r.o. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.ctoolkit.restapi.client.identity;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpStatusCodes;
import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.api.services.identitytoolkit.IdentityToolkit;
import com.google.identitytoolkit.HttpSender;
import com.google.identitytoolkit.JsonTokenHelper;
import com.google.identitytoolkit.RpcHelper;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import org.ctoolkit.restapi.client.RemoteServerErrorException;
import org.ctoolkit.restapi.client.UnauthorizedException;
import org.ctoolkit.restapi.client.googleapis.GoogleApiProxyFactory;
import org.ctoolkit.restapi.client.identity.verifier.VerifierModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
/**
* The Google Identity Toolkit guice module as a default configuration.
*
* @author Aurel Medvegy
*/
public class GoogleApiIdentityToolkitModule
extends AbstractModule
{
public static final String API_PREFIX = "identitytoolkit";
private static final Logger logger = LoggerFactory.getLogger( GoogleApiIdentityToolkitModule.class );
private static final String IDENTITY_SCOPE = "https://www.googleapis.com/auth/identitytoolkit";
@Override
protected void configure()
{
install( new VerifierModule() );
}
@Provides
@Singleton
IdentityToolkit provideIdentityToolkit( GoogleApiProxyFactory factory )
{
HashSet set = new HashSet<>();
set.add( IDENTITY_SCOPE );
Collections.unmodifiableSet( set );
Collection scopes = Collections.unmodifiableSet( set );
IdentityToolkit.Builder builder;
try
{
HttpRequestInitializer credential = factory.authorize( scopes, null, API_PREFIX );
builder = new IdentityToolkit.Builder( factory.getHttpTransport(), factory.getJsonFactory(), credential );
builder.setApplicationName( factory.getApplicationName( API_PREFIX ) );
}
catch ( GeneralSecurityException e )
{
logger.error( "Failed. Scopes: " + scopes.toString()
+ " Application name: " + factory.getApplicationName( API_PREFIX )
+ " Service account: " + factory.getServiceAccountEmail( API_PREFIX ), e );
throw new UnauthorizedException( e.getMessage() );
}
catch ( IOException e )
{
logger.error( "Failed. Scopes: " + scopes.toString()
+ " Application name: " + factory.getApplicationName( API_PREFIX )
+ " Service account: " + factory.getServiceAccountEmail( API_PREFIX ), e );
throw new RemoteServerErrorException( HttpStatusCodes.STATUS_CODE_SERVER_ERROR, e.getMessage() );
}
return builder.build();
}
@Provides
@Singleton
JsonTokenHelper provideJsonTokenHelper( GoogleApiProxyFactory factory, RpcHelper rpcHelper )
{
String projectId = factory.getProjectId( API_PREFIX );
if ( Strings.isNullOrEmpty( projectId ) )
{
throw new IllegalArgumentException( "Project ID (audience) must be provided, cannot be empty!" );
}
return new JsonTokenHelper( rpcHelper, projectId );
}
@Provides
@Singleton
RpcHelper provideRpcHelper( GoogleApiProxyFactory factory, Injector injector )
{
HttpSender sender = injector.getInstance( HttpSender.class );
InputStream stream = factory.getServiceAccountPrivateKeyP12Stream( API_PREFIX );
String serviceAccount = factory.getServiceAccountEmail( API_PREFIX );
return new RpcHelper( sender, IdentityToolkit.DEFAULT_BASE_URL, serviceAccount, stream );
}
}