Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 062e0863 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

convert IdentityProvider impl from java to kotlin to fix proguard issue

parent be67b39f
Loading
Loading
Loading
Loading
Loading
+0 −158
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2022, 2023
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package at.bitfire.davdroid.authorization;

import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.AuthorizationServiceConfiguration.RetrieveConfigurationCallback;

import java.util.Objects;

import at.bitfire.davdroid.BuildConfig;

/**
 * An abstraction of identity providers, containing all necessary info for the demo app.
 */
public class IdentityProvider {

    public static final IdentityProvider MURENA = new IdentityProvider(
            "https://accounts.eeo.one/auth/realms/eeo.one/.well-known/openid-configuration",
            null,
            null,
            BuildConfig.MURENA_CLIENT_ID,
            BuildConfig.MURENA_CLIENT_SECRET,
            BuildConfig.MURENA_REDIRECT_URI + ":/redirect",
            "openid address profile email phone roles offline_access web-origins microprofile-jwt",
            null
    );

    public static final IdentityProvider GOOGLE = new IdentityProvider(
            "https://accounts.google.com/.well-known/openid-configuration",
            null,
            null,
            BuildConfig.GOOGLE_CLIENT_ID,
            null,
            BuildConfig.GOOGLE_REDIRECT_URI + ":/oauth2redirect",
            "openid profile email https://www.googleapis.com/auth/carddav https://www.googleapis.com/auth/calendar https://mail.google.com/",
            null
    );

    public static final IdentityProvider YAHOO = new IdentityProvider(
            "https://api.login.yahoo.com/.well-known/openid-configuration",
            null,
            null,
            BuildConfig.YAHOO_CLIENT_ID,
            null,
            BuildConfig.APPLICATION_ID + "://oauth2redirect",
            "openid openid2 profile email mail-w sdct-w ycal-w",
            null
    );

    @Nullable
    private final Uri mDiscoveryEndpoint;

    @Nullable
    private final Uri mAuthEndpoint;

    @Nullable
    private final Uri mTokenEndpoint;
    @NonNull
    private final String mClientId;

    @Nullable
    private final String mClientSecret;
    @NonNull
    private final Uri mRedirectUri;

    @Nullable
    private final String mScope;

    @Nullable
    private final String mUserInfoEndpoint;

    IdentityProvider(
            @Nullable String discoveryEndpoint,
            @Nullable String authEndpoint,
            @Nullable String tokenEndpoint,
            @NonNull String clientId,
            @Nullable String clientSecret,
            @NonNull String redirectUri,
            @Nullable String scope,
            @Nullable String userInfoEndpoint) {
        if (discoveryEndpoint == null &&
                (authEndpoint == null || tokenEndpoint == null)) {
            throw new IllegalArgumentException(
                    "the discovery endpoint or the auth and token endpoints must be specified");
        }

        this.mDiscoveryEndpoint = retrieveUri(discoveryEndpoint);
        this.mAuthEndpoint = retrieveUri(authEndpoint);
        this.mTokenEndpoint = retrieveUri(tokenEndpoint);
        this.mClientId = clientId;
        this.mClientSecret = clientSecret;
        this.mRedirectUri = Objects.requireNonNull(retrieveUri(redirectUri));
        this.mScope = scope;
        this.mUserInfoEndpoint = userInfoEndpoint;
    }

    @NonNull
    public String getClientId() {
        return mClientId;
    }

    @Nullable
    public String getClientSecret() {
        return mClientSecret;
    }

    @NonNull
    public Uri getRedirectUri() {
        return mRedirectUri;
    }

    @NonNull
    public String getScope() {
        return mScope;
    }

    @Nullable
    public String getUserInfoEndpoint() {
        return mUserInfoEndpoint;
    }

    public void retrieveConfig(RetrieveConfigurationCallback callback) {
        if (mDiscoveryEndpoint != null) {
            AuthorizationServiceConfiguration.fetchFromUrl(mDiscoveryEndpoint, callback);
        } else {
            AuthorizationServiceConfiguration config =
                    new AuthorizationServiceConfiguration(mAuthEndpoint, mTokenEndpoint, null);
            callback.onFetchConfigurationCompleted(config, null);
        }
    }

    @Nullable
    private Uri retrieveUri(@Nullable String value) {
        if (value == null) {
            return null;
        }
        return Uri.parse(value);
    }
}
+108 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2022, 2023
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package at.bitfire.davdroid.authorization

import android.net.Uri
import at.bitfire.davdroid.BuildConfig
import net.openid.appauth.AuthorizationServiceConfiguration
import net.openid.appauth.AuthorizationServiceConfiguration.RetrieveConfigurationCallback

/**
 * An abstraction of identity providers, containing all necessary info for the demo app.
 */
enum class IdentityProvider(
    discoveryEndpoint: String?,
    authEndpoint: String?,
    tokenEndpoint: String?,
    clientId: String,
    clientSecret: String?,
    redirectUri: String,
    scope: String,
    userInfoEndpoint: String?
) {
    MURENA(
        "https://accounts.eeo.one/auth/realms/eeo.one/.well-known/openid-configuration",
        null,
        null,
        BuildConfig.MURENA_CLIENT_ID,
        BuildConfig.MURENA_CLIENT_SECRET,
        BuildConfig.MURENA_REDIRECT_URI + ":/redirect",
        "openid address profile email phone roles offline_access web-origins microprofile-jwt",
        null
    ),
    GOOGLE(
        "https://accounts.google.com/.well-known/openid-configuration",
        null,
        null,
        BuildConfig.GOOGLE_CLIENT_ID,
        null,
        BuildConfig.GOOGLE_REDIRECT_URI + ":/oauth2redirect",
        "openid profile email https://www.googleapis.com/auth/carddav https://www.googleapis.com/auth/calendar https://mail.google.com/",
        null
    ),
    YAHOO(
        "https://api.login.yahoo.com/.well-known/openid-configuration",
        null,
        null,
        BuildConfig.YAHOO_CLIENT_ID,
        null,
        BuildConfig.APPLICATION_ID + "://oauth2redirect",
        "openid openid2 profile email mail-w sdct-w ycal-w",
        null
    );

    private val mDiscoveryEndpoint: Uri?
    private val mAuthEndpoint: Uri?
    private val mTokenEndpoint: Uri?

    val clientId: String
    val clientSecret: String?
    val redirectUri: Uri
    val scope: String
    val userInfoEndpoint: String?

    init {
        require(
            !(discoveryEndpoint == null &&
                    (authEndpoint == null || tokenEndpoint == null))
        ) { "the discovery endpoint or the auth and token endpoints must be specified" }

        mDiscoveryEndpoint = retrieveUri(discoveryEndpoint)
        mAuthEndpoint = retrieveUri(authEndpoint)
        mTokenEndpoint = retrieveUri(tokenEndpoint)
        this.clientId = clientId
        this.clientSecret = clientSecret
        this.redirectUri =
            retrieveUri(redirectUri) ?: throw IllegalArgumentException("invalid redirect uri")
        this.scope = scope
        this.userInfoEndpoint = userInfoEndpoint
    }

    fun retrieveConfig(callback: RetrieveConfigurationCallback) {
        if (mDiscoveryEndpoint != null) {
            AuthorizationServiceConfiguration.fetchFromUrl(mDiscoveryEndpoint, callback)
        } else {
            val config = AuthorizationServiceConfiguration(mAuthEndpoint!!, mTokenEndpoint!!, null)
            callback.onFetchConfigurationCompleted(config, null)
        }
    }

    private fun retrieveUri(value: String?): Uri? {
        return if (value == null) {
            null
        } else Uri.parse(value)
    }
}
 No newline at end of file