Search in sources :

Example 1 with OAuthRequest

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;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest) PermissionDto(io.jawg.osmcontributor.rest.dtos.osm.PermissionDto) IOException(java.io.IOException)

Example 2 with OAuthRequest

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());
    }
}
Also used : OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest) Request(okhttp3.Request) OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest)

Example 3 with OAuthRequest

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;
    }
}
Also used : OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest)

Example 4 with OAuthRequest

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();
    }
}
Also used : OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest) Request(okhttp3.Request) OAuthRequest(io.jawg.osmcontributor.rest.security.OAuthRequest) Nullable(javax.annotation.Nullable)

Aggregations

OAuthRequest (io.jawg.osmcontributor.rest.security.OAuthRequest)4 Request (okhttp3.Request)2 OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)1 PermissionDto (io.jawg.osmcontributor.rest.dtos.osm.PermissionDto)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1