001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.oauth;
003
004import java.util.stream.Stream;
005
006/**
007 * A generic interface for OAuth Parameters
008 * @author Taylor Smock
009 * @since 18650
010 */
011public interface IOAuthParameters {
012    /**
013     * Get the access token URL
014     * @return The URL to use to switch the code to a token
015     */
016    String getAccessTokenUrl();
017
018    /**
019     * Get the base authorization URL to open in a browser
020     * @return The base URL to send to the browser
021     */
022    String getAuthorizationUrl();
023
024    /**
025     * Get the authorization URL to open in a browser
026     * @param state The state to prevent attackers from providing their own token
027     * @param scopes The scopes to request
028     * @return The URL to send to the browser
029     */
030    default String getAuthorizationUrl(String state, Enum<?>... scopes) {
031        return this.getAuthorizationUrl(state, Stream.of(scopes).map(Enum::toString).toArray(String[]::new));
032    }
033
034    /**
035     * Get the authorization URL to open in a browser
036     * @param state The state to prevent attackers from providing their own token
037     * @param scopes The scopes to request
038     * @return The URL to send to the browser
039     */
040    default String getAuthorizationUrl(String state, String... scopes) {
041        // response_type = code | token, but token is deprecated in the draft oauth 2.1 spec
042        // 2.1 is adding code_challenge, code_challenge_method
043        // code_challenge requires a code_verifier
044        return this.getAuthorizationUrl() + "?response_type=code&client_id=" + this.getClientId()
045                + "&redirect_uri=" + this.getRedirectUri()
046                + "&scope=" + String.join(" ", scopes)
047                // State is used to detect/prevent cross-site request forgery
048                + "&state=" + state;
049    }
050
051    /**
052     * Get the OAuth version that the API expects
053     * @return The oauth version
054     */
055    OAuthVersion getOAuthVersion();
056
057    /**
058     * Get the client id
059     * @return The client id
060     */
061    String getClientId();
062
063    /**
064     * Get the client secret
065     * @return The client secret
066     */
067    String getClientSecret();
068
069    /**
070     * Get the redirect URI
071     * @return The redirect URI
072     */
073    default String getRedirectUri() {
074        return null;
075    }
076
077    /**
078     * Convert to a preference string
079     * @return the preference string
080     */
081    default String toPreferencesString() {
082        return null;
083    }
084
085    /**
086     * Get the actual API URL
087     * @return The API URl
088     */
089    default String getApiUrl() {
090        return null;
091    }
092
093    /**
094     * Store the preferences for these parameters
095     */
096    void rememberPreferences();
097}