Search in sources :

Example 1 with OauthServer

use of net.geoprism.account.OauthServer in project geoprism-registry by terraframe.

the class DHIS2ExternalSystem method delete.

@Override
public void delete() {
    OauthServer oauth = this.getOauthServer();
    super.delete();
    if (oauth != null) {
        oauth.delete();
    }
}
Also used : OauthServer(net.geoprism.account.OauthServer)

Example 2 with OauthServer

use of net.geoprism.account.OauthServer in project geoprism-registry by terraframe.

the class FhirExternalSystem method delete.

@Override
public void delete() {
    OauthServer oauth = this.getOauthServer();
    super.delete();
    if (oauth != null) {
        oauth.delete();
    }
}
Also used : OauthServer(net.geoprism.account.OauthServer)

Example 3 with OauthServer

use of net.geoprism.account.OauthServer in project geoprism-registry by terraframe.

the class RegistrySessionService method ologin.

/**
 * Serves as a "redirect url" for logging into DHIS2 via oauth.
 *
 * @param serverId
 * @param code
 * @param locales
 * @param redirectBase
 * @return
 */
@Authenticate
public static java.lang.String ologin(java.lang.String serverId, java.lang.String code, java.lang.String locales, java.lang.String redirectBase) {
    try {
        // We used to try to build this from the controller but it would include stuff (like the port :443) which then wouldn't match
        // with the redirect url the client specified in DHIS2. Therefore this has to be something that the user can set (or, at least,
        // in a properties file)
        redirectBase = GeoregistryProperties.getRemoteServerUrl();
        String redirect = redirectBase + "cgrsession/ologin";
        OauthServer server = OauthServer.get(serverId);
        /*
       * Get the access token
       */
        TokenRequestBuilder tokenBuilder = OAuthClientRequest.tokenLocation(server.getTokenLocation());
        tokenBuilder.setGrantType(GrantType.AUTHORIZATION_CODE);
        tokenBuilder.setRedirectURI(redirect);
        tokenBuilder.setCode(code);
        String auth = server.getClientId() + ":" + server.getSecretKey();
        OAuthClientRequest tokenRequest = tokenBuilder.buildBodyMessage();
        tokenRequest.setHeader("Accept", "application/json");
        tokenRequest.setHeader("Authorization", "Basic " + new String(Base64.getEncoder().encode(auth.getBytes())));
        URLConnectionClient connClient = new URLConnectionClient();
        OAuthClient oAuthClient = new OAuthClient(connClient);
        OAuthJSONAccessTokenResponse accessToken = oAuthClient.accessToken(tokenRequest, OAuth.HttpMethod.POST, OAuthJSONAccessTokenResponse.class);
        /*
       * Request the user information
       */
        OAuthBearerClientRequest requestBuilder = new OAuthBearerClientRequest(server.getProfileLocation());
        requestBuilder.setAccessToken(accessToken.getAccessToken());
        OAuthClientRequest bearerRequest = requestBuilder.buildQueryMessage();
        OAuthResourceResponse resourceResponse = oAuthClient.resource(bearerRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
        String body = resourceResponse.getBody();
        JSONObject object = new JSONObject(body);
        final String username = object.getJSONObject("userCredentials").getString("username");
        SingleActorDAOIF profile = RegistrySessionService.getActor(server, username);
        String sessionId = SessionFacade.logIn(profile, LocaleSerializer.deserialize(locales));
        JsonObject json = new JsonObject();
        json.addProperty("sessionId", sessionId);
        json.addProperty("username", username);
        return json.toString();
    } catch (JSONException | OAuthSystemException | OAuthProblemException e) {
        throw new InvalidLoginException(e);
    }
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) JsonObject(com.google.gson.JsonObject) JSONException(org.json.JSONException) OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) JSONObject(org.json.JSONObject) InvalidLoginException(com.runwaysdk.session.InvalidLoginException) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) SingleActorDAOIF(com.runwaysdk.business.rbac.SingleActorDAOIF) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OauthServer(net.geoprism.account.OauthServer) Authenticate(com.runwaysdk.business.rbac.Authenticate)

Example 4 with OauthServer

use of net.geoprism.account.OauthServer in project geoprism-registry by terraframe.

the class OauthExternalSystem method updateOauthServer.

public default void updateOauthServer(JsonObject jo) {
    if (jo.has(OauthExternalSystem.OAUTH_SERVER) && !jo.get(OauthExternalSystem.OAUTH_SERVER).isJsonNull()) {
        Gson gson2 = new GsonBuilder().registerTypeAdapter(OauthServer.class, new RunwayJsonAdapters.RunwayDeserializer()).create();
        OauthServer oauth = gson2.fromJson(jo.get(OauthExternalSystem.OAUTH_SERVER), OauthServer.class);
        OauthServer dbServer = this.getOauthServer();
        if (dbServer != null) {
            dbServer.lock();
            dbServer.populate(oauth);
            oauth = dbServer;
        }
        String systemLabel = this.getLocalizedLabel().getValue();
        oauth.getDisplayLabel().setValue(systemLabel);
        oauth.apply();
        this.setOauthServer(oauth);
        this.apply();
    } else if (this.getOauthServer() != null) {
        OauthServer existingOauth = this.getOauthServer();
        this.setOauthServerId(null);
        this.apply();
        existingOauth.delete();
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) OauthServer(net.geoprism.account.OauthServer)

Example 5 with OauthServer

use of net.geoprism.account.OauthServer in project geoprism-registry by terraframe.

the class ExternalSystemService method remove.

@Request(RequestType.SESSION)
public void remove(String sessionId, String oid) {
    ExternalSystem system = ExternalSystem.get(oid);
    Organization organization = system.getOrganization();
    ServiceFactory.getRolePermissionService().enforceRA(organization.getCode());
    if (system instanceof DHIS2ExternalSystem) {
        DHIS2ExternalSystem dhis2Sys = (DHIS2ExternalSystem) system;
        if (dhis2Sys.getOauthServer() != null) {
            OauthServer dbServer = dhis2Sys.getOauthServer();
            dbServer.delete();
        }
    }
    system.delete();
}
Also used : Organization(net.geoprism.registry.Organization) DHIS2ExternalSystem(net.geoprism.registry.graph.DHIS2ExternalSystem) FhirExternalSystem(net.geoprism.registry.graph.FhirExternalSystem) DHIS2ExternalSystem(net.geoprism.registry.graph.DHIS2ExternalSystem) ExternalSystem(net.geoprism.registry.graph.ExternalSystem) OauthExternalSystem(net.geoprism.registry.etl.OauthExternalSystem) OauthServer(net.geoprism.account.OauthServer) Request(com.runwaysdk.session.Request)

Aggregations

OauthServer (net.geoprism.account.OauthServer)8 Request (com.runwaysdk.session.Request)4 JsonObject (com.google.gson.JsonObject)3 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 JsonArray (com.google.gson.JsonArray)2 Authenticate (com.runwaysdk.business.rbac.Authenticate)1 SingleActorDAOIF (com.runwaysdk.business.rbac.SingleActorDAOIF)1 InvalidLoginException (com.runwaysdk.session.InvalidLoginException)1 Organization (net.geoprism.registry.Organization)1 OauthExternalSystem (net.geoprism.registry.etl.OauthExternalSystem)1 DHIS2ExternalSystem (net.geoprism.registry.graph.DHIS2ExternalSystem)1 ExternalSystem (net.geoprism.registry.graph.ExternalSystem)1 FhirExternalSystem (net.geoprism.registry.graph.FhirExternalSystem)1 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)1 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)1 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)1 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)1 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)1