Search in sources :

Example 1 with PermissionDto

use of io.jawg.osmcontributor.rest.dtos.osm.PermissionDto 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)

Aggregations

OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)1 PermissionDto (io.jawg.osmcontributor.rest.dtos.osm.PermissionDto)1 OAuthRequest (io.jawg.osmcontributor.rest.security.OAuthRequest)1 IOException (java.io.IOException)1