Search in sources :

Example 1 with OAuthClientRegistrationResponse

use of org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method registerApplication.

@Override
public Long registerApplication(String registrationEndpoint, String apiUrl, String redirectUrl) throws UserException, ServerException {
    try {
        try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
            OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint);
            if (oAuthServer != null) {
                return oAuthServer.getOid();
            }
            ServerSettings serverSettings = getBimServer().getServerSettingsCache().getServerSettings();
            OAuthClientRequest request = OAuthClientRegistrationRequest.location(registrationEndpoint, OAuthRegistration.Type.PUSH).setName(serverSettings.getName()).setUrl(redirectUrl).setDescription(serverSettings.getDescription()).setIcon(serverSettings.getIcon()).setRedirectURL(redirectUrl).buildJSONMessage();
            OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new URLConnectionClient());
            OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
            oAuthServer = session.create(OAuthServer.class);
            oAuthServer.setApiUrl(apiUrl);
            oAuthServer.setClientId(response.getClientId());
            oAuthServer.setClientSecret(response.getClientSecret());
            oAuthServer.setIssuedAt(new Date(Long.parseLong(response.getIssuedAt())));
            GregorianCalendar expiresAt = new GregorianCalendar();
            expiresAt.setTimeInMillis(new GregorianCalendar().getTimeInMillis() + response.getExpiresIn());
            oAuthServer.setExpiresAt(expiresAt.getTime());
            oAuthServer.setRegistrationEndpoint(registrationEndpoint);
            oAuthServer.setClientDescription(serverSettings.getDescription());
            oAuthServer.setClientName(serverSettings.getName());
            if (serverSettings.getIcon() != null) {
                byte[] icon = NetUtils.getContentAsBytes(new URL(serverSettings.getIcon()), 500);
                oAuthServer.setClientIcon(icon);
            }
            oAuthServer.setIncoming(false);
            oAuthServer.setRedirectUrl(redirectUrl);
            session.commit();
            return oAuthServer.getOid();
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) DatabaseSession(org.bimserver.database.DatabaseSession) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) ServerSettings(org.bimserver.models.store.ServerSettings) GregorianCalendar(java.util.GregorianCalendar) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) Date(java.util.Date) URL(java.net.URL) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 2 with OAuthClientRegistrationResponse

use of org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method registerRemoteApplication.

public SOAuthServer registerRemoteApplication(String redirectUrl, String name, String description) throws UserException {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location(getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/oauth/register/", OAuthRegistration.Type.PUSH).setName(name).setUrl(redirectUrl).setDescription(description).setRedirectURL(redirectUrl).buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        SOAuthServer server = new SOAuthServer();
        server.setClientId(response.getClientId());
        server.setClientSecret(response.getClientSecret());
        return server;
    } catch (Exception e) {
        throw new UserException(e);
    }
}
Also used : OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) UserException(org.bimserver.shared.exceptions.UserException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 3 with OAuthClientRegistrationResponse

use of org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse in project BIMserver by opensourceBIM.

the class SendUrl method main.

public static void main(String[] args) {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location("https://thisisanexperimentalserver.com/oauth/register/", OAuthRegistration.Type.PUSH).setName("Zapier").setUrl("https://zapier.com/dashboard/auth/oauth/return/App56192API").setDescription("App Description").setRedirectURL("https://zapier.com/dashboard/auth/oauth/return/App56192API").buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        System.out.println(response.getClientId());
        System.out.println(response.getClientSecret());
    } catch (OAuthSystemException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OAuthProblemException e) {
        e.printStackTrace();
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)3 OAuthRegistrationClient (org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient)3 OAuthClientRegistrationResponse (org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse)3 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)2 SOAuthServer (org.bimserver.interfaces.objects.SOAuthServer)2 ServerException (org.bimserver.shared.exceptions.ServerException)2 UserException (org.bimserver.shared.exceptions.UserException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 DatabaseSession (org.bimserver.database.DatabaseSession)1 OAuthServer (org.bimserver.models.store.OAuthServer)1 ServerSettings (org.bimserver.models.store.ServerSettings)1