use of com.bluenimble.platform.remote.impls.http.oauth.OkHttpOAuthConsumer in project serverless by bluenimble.
the class HttpRemote method sign.
private Request sign(Request request, JsonObject spec, List<RequestParameter> parameters) throws Exception {
if (!spec.containsKey(Spec.Sign)) {
return request;
}
Object sign = Json.getObject(spec, Spec.Sign);
if (!(sign instanceof JsonObject)) {
return request;
}
JsonObject oSign = (JsonObject) sign;
String signer = Json.getString(oSign, Spec.SignProtocol, Signers.OAuth).toLowerCase();
if (Signers.OAuth.equals(signer)) {
String key = Json.getString(oSign, Spec.SignKey);
if (Lang.isNullOrEmpty(key)) {
throw new Exception("oauth consumer key not found in spec");
}
String secret = Json.getString(oSign, Spec.SignSecret);
if (Lang.isNullOrEmpty(secret)) {
throw new Exception("oauth consumer secret not found in spec");
}
OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(key, secret);
String token = Json.getString(oSign, Spec.SignToken);
if (!Lang.isNullOrEmpty(token)) {
String tokenSecret = Json.getString(oSign, Spec.SignTokenSecret);
if (Lang.isNullOrEmpty(tokenSecret)) {
throw new Exception("oauth token secret not found in spec");
}
consumer.setTokenWithSecret(token, secret);
}
return (Request) consumer.sign(request).unwrap();
} else if (Signers.Bnb.equals(signer)) {
// bnb sign
String key = Json.getString(oSign, Spec.SignKey);
if (Lang.isNullOrEmpty(key)) {
throw new Exception("bnb key not found in spec");
}
String secret = Json.getString(oSign, Spec.SignSecret);
if (Lang.isNullOrEmpty(secret)) {
throw new Exception("bnb secret not found in spec");
}
return BnBSigner.sign(request, parameters, oSign);
} else if (Signers.Basic.equals(signer)) {
// bnb sign
String user = Json.getString(oSign, Spec.User);
if (Lang.isNullOrEmpty(user)) {
throw new Exception("basic-auth user not found in spec");
}
String password = Json.getString(oSign, Spec.Password);
if (Lang.isNullOrEmpty(password)) {
throw new Exception("basic-auth password not found in spec");
}
return request.newBuilder().header(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encodeBase64String((user + Lang.COLON + password).getBytes())).trim()).build();
} else {
throw new Exception("unsupported signature protocol " + signer);
}
}
Aggregations