Search in sources :

Example 1 with DatabaseObject

use of com.bluenimble.platform.db.DatabaseObject in project serverless by bluenimble.

the class BasicConsumerResolver method authorize.

@Override
public ApiConsumer authorize(Api api, ApiService service, ApiRequest request, ApiConsumer consumer) throws ApiAuthenticationException {
    JsonObject auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), MethodName), Api.Spec.Security.Auth);
    if (auth == null || auth.isEmpty()) {
        return consumer;
    }
    String feature = Json.getString(auth, Spec.Auth.Feature);
    JsonObject query = Json.getObject(auth, Spec.Auth.Query);
    if (query == null || query.isEmpty()) {
        return consumer;
    }
    Map<String, Object> bindings = new HashMap<String, Object>();
    bindings.put(ApiConsumer.Fields.Id, consumer.get(ApiConsumer.Fields.Id));
    bindings.put(ApiConsumer.Fields.Password, consumer.get(ApiConsumer.Fields.Password));
    JsonQuery q = new JsonQuery(query, bindings);
    DatabaseObject odb = null;
    try {
        odb = api.space().feature(Database.class, feature, request).findOne(null, q);
    } catch (Exception ex) {
        throw new ApiAuthenticationException(ex.getMessage(), ex);
    }
    boolean isServiceSecure = Json.getBoolean(service.getSecurity(), ApiService.Spec.Security.Enabled, true);
    if (odb == null) {
        if (isServiceSecure) {
            throw new ApiAuthenticationException("invalid user/password");
        } else {
            return consumer;
        }
    }
    JsonObject oConsumer = odb.toJson(null);
    for (Object k : oConsumer.keySet()) {
        consumer.set(String.valueOf(k), oConsumer.get(k));
    }
    consumer.set(ApiConsumer.Fields.Anonymous, false);
    return consumer;
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) HashMap(java.util.HashMap) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException)

Example 2 with DatabaseObject

use of com.bluenimble.platform.db.DatabaseObject in project serverless by bluenimble.

the class SignatureConsumerResolver method getSecretKey.

private String getSecretKey(Api api, ApiRequest request, ApiConsumer consumer, String accessKey) throws ApiAuthenticationException {
    JsonObject auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), MethodName), Api.Spec.Security.Auth);
    if (auth == null || auth.isEmpty()) {
        return null;
    }
    String feature = Json.getString(auth, Spec.Auth.Feature);
    String secretKeyField = Json.getString(auth, Spec.Auth.SecretKeyField, Defaults.SecretKey);
    JsonObject query = Json.getObject(auth, Spec.Auth.Query);
    JsonArray parameters = Json.getArray(auth, Spec.Auth.Parameters);
    if (query == null || query.isEmpty()) {
        return null;
    }
    Map<String, Object> bindings = new HashMap<String, Object>();
    bindings.put(ApiConsumer.Fields.AccessKey, accessKey);
    // addt params
    if (parameters != null && !parameters.isEmpty()) {
        for (int i = 0; i < parameters.count(); i++) {
            String key = String.valueOf(parameters.get(i));
            Object o = request.get(key);
            if (o != null) {
                bindings.put(key, o);
            }
        }
    }
    JsonQuery q = new JsonQuery(query, bindings);
    DatabaseObject odb = null;
    try {
        odb = api.space().feature(Database.class, feature, request).findOne(null, q);
    } catch (Exception ex) {
        throw new ApiAuthenticationException(ex.getMessage(), ex);
    }
    if (odb == null) {
        throw new ApiAuthenticationException("invalid accessKey " + accessKey);
    }
    JsonObject oRecord = odb.toJson(null);
    String[] secretKeyProps = Lang.split(secretKeyField, Lang.DOT);
    Object oSecretKey = Json.find(oRecord, secretKeyProps);
    if (oSecretKey == null) {
        throw new ApiAuthenticationException("secret key not found for accessKey " + accessKey);
    }
    if (!(oSecretKey instanceof String)) {
        throw new ApiAuthenticationException("secret key should be a valid String");
    }
    consumer.set(ApiConsumer.Fields.AccessKey, accessKey);
    consumer.set(ApiConsumer.Fields.SecretKey, oSecretKey);
    JsonObject oConsumer = oRecord;
    for (Object k : oConsumer.keySet()) {
        consumer.set(String.valueOf(k), oConsumer.get(k));
    }
    consumer.set(ApiConsumer.Fields.Anonymous, false);
    return (String) oSecretKey;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) HashMap(java.util.HashMap) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) ParseException(java.text.ParseException)

Example 3 with DatabaseObject

use of com.bluenimble.platform.db.DatabaseObject in project serverless by bluenimble.

the class GetRecordSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    String record = (String) request.get(Spec.Record);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.get(sEntity, record);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (dbo == null) {
        return null;
    }
    return new JsonApiOutput(dbo.toJson(null));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 4 with DatabaseObject

use of com.bluenimble.platform.db.DatabaseObject in project serverless by bluenimble.

the class AddRecordSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.create(sEntity);
        dbo.load(payload);
        dbo.save();
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(ApiOutput.Defaults.Id, dbo.getId()).set(ApiOutput.Defaults.Timestamp, Lang.toUTC(dbo.getTimestamp())));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 5 with DatabaseObject

use of com.bluenimble.platform.db.DatabaseObject in project serverless by bluenimble.

the class LoginServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    JsonObject config = request.getService().getCustom();
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    Database db = api.space().feature(Database.class, Json.getString(config, Config.Database, ApiSpace.Features.Default), request);
    boolean encryptPassword = Json.getBoolean(config, Config.EncryptPassword, true);
    DatabaseObject account = null;
    try {
        JsonObject query = Json.getObject(config, Config.Query);
        if (query == null) {
            query = new JsonObject();
            JsonObject where = new JsonObject();
            query.set(Query.Construct.where.name(), where);
            where.set(Json.getString(config, Config.UserProperty, Fields.Email), payload.get(Spec.User));
            where.set(Json.getString(config, Config.PasswordProperty, Fields.Password), encryptPassword ? Crypto.md5(Json.getString(payload, Spec.Password), Encodings.UTF8) : Json.getString(payload, Spec.Password));
        }
        account = db.findOne(Json.getString(config, Config.UsersEntity, Defaults.Users), new JsonQuery(query));
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (account == null) {
        throw new ApiServiceExecutionException("account not found").status(ApiResponse.UNAUTHORIZED);
    }
    boolean active = true;
    boolean requiresActivation = Json.getBoolean(config, Config.RequiresActivation, false);
    if (requiresActivation && account.get(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode)) != null) {
        active = false;
    }
    JsonObject oAccount = account.toJson(DefaultDatabaseObjectSerializer.Default);
    oAccount.remove(Json.getString(config, Config.PasswordProperty, Spec.Password));
    if (active) {
        Date now = new Date();
        // update lastLogin
        try {
            account.set(Json.getString(config, Config.LastLoginProperty, Fields.LastLogin), now);
            account.save();
        } catch (Exception ex) {
            throw new ApiServiceExecutionException(ex.getMessage(), ex);
        }
        // create token
        String[] tokenAndExpiration = SecurityUtils.tokenAndExpiration(api, oAccount, now);
        oAccount.set(Defaults.Token, tokenAndExpiration[0]);
        oAccount.set(Defaults.ExpiresOn, tokenAndExpiration[1]);
    }
    // call extend if any
    JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
    ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAccount);
    oAccount.remove(Database.Fields.Id);
    if (onFinishOutput != null) {
        oAccount.set(Json.getString(onFinish, Config.onFinish.ResultProperty, Config.onFinish.class.getSimpleName()), onFinishOutput.data());
    }
    return new JsonApiOutput(oAccount);
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Date(java.util.Date) ApiOutput(com.bluenimble.platform.api.ApiOutput) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

DatabaseObject (com.bluenimble.platform.db.DatabaseObject)42 Database (com.bluenimble.platform.db.Database)37 JsonObject (com.bluenimble.platform.json.JsonObject)29 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)20 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)9 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)9 DatabaseException (com.bluenimble.platform.db.DatabaseException)6 JsonArray (com.bluenimble.platform.json.JsonArray)6 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiOutput (com.bluenimble.platform.api.ApiOutput)4 ApiSpace (com.bluenimble.platform.api.ApiSpace)4 Date (java.util.Date)4 HashMap (java.util.HashMap)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 Messenger (com.bluenimble.platform.messaging.Messenger)2 JsonRecipient (com.bluenimble.platform.messaging.impls.JsonRecipient)2 JsonSender (com.bluenimble.platform.messaging.impls.JsonSender)2 OIntentMassiveInsert (com.orientechnologies.orient.core.intent.OIntentMassiveInsert)1