use of io.jawg.osmcontributor.rest.security.OAuthRequest in project osm-contributor by jawg.
the class StoreLoginManager method isValidLogin.
/**
* Calls the permissions web service to determine if the user provided has editing capabilities
*
* @return editing capabilities or not (false also if an retrofit error occurred)
*/
@Override
public boolean isValidLogin(final String login, final String password) {
try {
Call<OsmDto> callPerms;
OsmDto permissions;
Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
// OAuth connection
if (oAuthParams != null) {
String requestUrl = BuildConfig.BASE_OSM_URL + "permissions";
OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
oAuthRequest.setRequestUrl(requestUrl);
oAuthRequest.signRequest(Verb.GET);
oAuthRequest.encodeParams();
String headerRequest = oAuthRequest.getOAuthHeader();
callPerms = osmRestClient.getPermissions(headerRequest);
} else {
// Basic Auth connection
String authorization = "Basic " + Base64.encodeToString((login.trim() + ":" + password).getBytes(), Base64.NO_WRAP);
callPerms = osmRestClient.getPermissions(authorization);
}
Response<OsmDto> response = callPerms.execute();
if (response.isSuccessful()) {
permissions = response.body();
if (permissions != null) {
if (permissions.getPermissionsDto() != null && permissions.getPermissionsDto().getPermissionDtoList() != null) {
for (PermissionDto permissionDto : permissions.getPermissionsDto().getPermissionDtoList()) {
if ("allow_write_api".equals(permissionDto.getName())) {
return true;
}
}
}
}
}
} catch (IOException e) {
Timber.e("Couldn't request permissions " + e);
}
return false;
}
use of io.jawg.osmcontributor.rest.security.OAuthRequest in project osm-contributor by jawg.
the class AuthorisationInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
if (oAuthParams != null) {
Request originalRequest = chain.request();
String requestUrl = originalRequest.url().toString();
OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
oAuthRequest.setRequestUrl(requestUrl);
oAuthRequest.signRequest(Verb.valueOf(originalRequest.method()));
oAuthRequest.encodeParams();
Request.Builder finalRequest = originalRequest.newBuilder().addHeader("Authorization", oAuthRequest.getOAuthHeader()).method(originalRequest.method(), originalRequest.body());
return chain.proceed(finalRequest.build());
} else {
// create Base64 encoded string
String authorization = "Basic " + Base64.encodeToString((loginPreferences.retrieveLogin() + ":" + loginPreferences.retrievePassword()).getBytes(), Base64.NO_WRAP);
Timber.i("AUTHORIZATION TEST");
return chain.proceed(chain.request().newBuilder().header("Authorization", authorization).header("Accept", "text/xml").build());
}
}
use of io.jawg.osmcontributor.rest.security.OAuthRequest in project osm-contributor by jawg.
the class AuthenticationRequestInterceptor method getOAuthRequest.
public static OAuthRequest getOAuthRequest(LoginPreferences loginPreferences, String requestUrl, Verb verb) {
Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
if (oAuthParams != null) {
OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
oAuthRequest.setRequestUrl(requestUrl);
oAuthRequest.signRequest(verb);
oAuthRequest.encodeParams();
return oAuthRequest;
} else {
return null;
}
}
use of io.jawg.osmcontributor.rest.security.OAuthRequest in project osm-contributor by jawg.
the class AuthenticationRequestInterceptor method authenticate.
@Nullable
@Override
public Request authenticate(Route proxy, Response response) throws IOException {
Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
if (response.request().header("Authorization") != null) {
return null;
}
if (oAuthParams != null) {
Request originalRequest = response.request();
String requestUrl = originalRequest.toString();
OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
oAuthRequest.setRequestUrl(requestUrl);
oAuthRequest.signRequest(Verb.valueOf(originalRequest.method()));
oAuthRequest.encodeParams();
Request.Builder finalRequest = originalRequest.newBuilder().addHeader("Authorization", oAuthRequest.getOAuthHeader()).method(originalRequest.method(), originalRequest.body());
return finalRequest.build();
} else {
// create Base64 encoded string
String authorization = "Basic " + Base64.encodeToString((loginPreferences.retrieveLogin() + ":" + loginPreferences.retrievePassword()).getBytes(), Base64.NO_WRAP);
return response.request().newBuilder().header("Authorization", authorization).header("Accept", "text/xml").build();
}
}
Aggregations