Search in sources :

Example 1 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery 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 JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery 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 JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class TestWithBindingsQueryCompiler method main.

public static void main(String[] args) throws Exception {
    Map<String, Object> bindings = new HashMap<String, Object>();
    bindings.put("alpha", "alpha-val");
    bindings.put("beta", new Date());
    Query query = new JsonQuery(Json.load(new File("tests/queries/with-bindings.json")), bindings);
    System.out.println("Select==>");
    QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select);
    CompiledQuery cq = sc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
    System.out.println("Delete==>");
    QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
    cq = dc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
}
Also used : CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) Query(com.bluenimble.platform.db.query.Query) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) HashMap(java.util.HashMap) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) QueryCompiler(com.bluenimble.platform.db.query.QueryCompiler) File(java.io.File) Date(java.util.Date) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Example 4 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery in project serverless by bluenimble.

the class TestQueryCompilerWithOperators method main.

public static void main(String[] args) throws Exception {
    Query query = new JsonQuery(Json.load(new File("tests/queries/with-operators.json")));
    System.out.println("Select==>");
    QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select);
    CompiledQuery cq = sc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
    System.out.println("Delete==>");
    QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
    cq = dc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
}
Also used : CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) Query(com.bluenimble.platform.db.query.Query) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) QueryCompiler(com.bluenimble.platform.db.query.QueryCompiler) File(java.io.File) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Example 5 with JsonQuery

use of com.bluenimble.platform.db.query.impls.JsonQuery 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

JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)20 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)16 JsonObject (com.bluenimble.platform.json.JsonObject)16 Database (com.bluenimble.platform.db.Database)14 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)10 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)4 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)4 CompiledQuery (com.bluenimble.platform.db.query.CompiledQuery)4 Query (com.bluenimble.platform.db.query.Query)4 QueryCompiler (com.bluenimble.platform.db.query.QueryCompiler)4 SqlQueryCompiler (com.bluenimble.platform.db.query.impls.SqlQueryCompiler)4 File (java.io.File)4 Date (java.util.Date)4 ApiOutput (com.bluenimble.platform.api.ApiOutput)3 JsonArray (com.bluenimble.platform.json.JsonArray)3 HashMap (java.util.HashMap)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 DatabaseException (com.bluenimble.platform.db.DatabaseException)2 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)1