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;
}
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;
}
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));
}
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())));
}
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);
}
Aggregations