001/*
002 * Copyright 2016 The AppAuth for Android Authors. All Rights Reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the
010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
011 * express or implied. See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package net.openid.appauth;
016
017import androidx.annotation.NonNull;
018
019import net.openid.appauth.browser.AnyBrowserMatcher;
020import net.openid.appauth.browser.BrowserMatcher;
021import net.openid.appauth.connectivity.ConnectionBuilder;
022import net.openid.appauth.connectivity.DefaultConnectionBuilder;
023
024/**
025 * Defines configuration properties that control the behavior of the AppAuth library, independent
026 * of the OAuth2 specific details that are described.
027 */
028public class AppAuthConfiguration {
029
030    /**
031     * The default configuration that is used if no configuration is explicitly specified
032     * when constructing an {@link AuthorizationService}.
033     */
034    public static final AppAuthConfiguration DEFAULT =
035            new AppAuthConfiguration.Builder().build();
036
037    @NonNull
038    private final BrowserMatcher mBrowserMatcher;
039
040    @NonNull
041    private final ConnectionBuilder mConnectionBuilder;
042
043    private final boolean mSkipIssuerHttpsCheck;
044
045    private AppAuthConfiguration(
046            @NonNull BrowserMatcher browserMatcher,
047            @NonNull ConnectionBuilder connectionBuilder,
048            Boolean skipIssuerHttpsCheck) {
049        mBrowserMatcher = browserMatcher;
050        mConnectionBuilder = connectionBuilder;
051        mSkipIssuerHttpsCheck = skipIssuerHttpsCheck;
052    }
053
054    /**
055     * Controls which browsers can be used for the authorization flow.
056     */
057    @NonNull
058    public BrowserMatcher getBrowserMatcher() {
059        return mBrowserMatcher;
060    }
061
062    /**
063     * Creates {@link java.net.HttpURLConnection} instances for use in token requests and related
064     * interactions with the authorization service.
065     */
066    @NonNull
067    public ConnectionBuilder getConnectionBuilder() {
068        return mConnectionBuilder;
069    }
070
071    /**
072     * Returns <code>true</code> if issuer https validation is disabled, otherwise
073     * <code>false</code>.
074     *
075     * @see Builder#setSkipIssuerHttpsCheck(Boolean)
076     */
077    public boolean getSkipIssuerHttpsCheck() { return mSkipIssuerHttpsCheck; }
078
079    /**
080     * Creates {@link AppAuthConfiguration} instances.
081     */
082    public static class Builder {
083
084        private BrowserMatcher mBrowserMatcher = AnyBrowserMatcher.INSTANCE;
085        private ConnectionBuilder mConnectionBuilder = DefaultConnectionBuilder.INSTANCE;
086        private boolean mSkipIssuerHttpsCheck;
087        private boolean mSkipNonceVerification;
088
089        /**
090         * Specify the browser matcher to use, which controls the browsers that can be used
091         * for authorization.
092         */
093        @NonNull
094        public Builder setBrowserMatcher(@NonNull BrowserMatcher browserMatcher) {
095            Preconditions.checkNotNull(browserMatcher, "browserMatcher cannot be null");
096            mBrowserMatcher = browserMatcher;
097            return this;
098        }
099
100        /**
101         * Specify the connection builder to use, which creates {@link java.net.HttpURLConnection}
102         * instances for use in direct communication with the authorization service.
103         */
104        @NonNull
105        public Builder setConnectionBuilder(@NonNull ConnectionBuilder connectionBuilder) {
106            Preconditions.checkNotNull(connectionBuilder, "connectionBuilder cannot be null");
107            mConnectionBuilder = connectionBuilder;
108            return this;
109        }
110
111        /**
112         * Disables https validation for the issuer identifier.
113         *
114         * <p>NOTE: Disabling issuer https validation implies the app is running against an
115         * insecure environment. Enabling this option is only recommended for testing purposes.
116         */
117        public Builder setSkipIssuerHttpsCheck(Boolean skipIssuerHttpsCheck) {
118            mSkipIssuerHttpsCheck = skipIssuerHttpsCheck;
119            return this;
120        }
121
122        /**
123         * Creates the instance from the configured properties.
124         */
125        @NonNull
126        public AppAuthConfiguration build() {
127            return new AppAuthConfiguration(
128                mBrowserMatcher,
129                mConnectionBuilder,
130                mSkipIssuerHttpsCheck
131            );
132        }
133
134
135    }
136}