| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.oauth;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.stream.Stream;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * A generic interface for OAuth Parameters
|
|---|
| 8 | * @author Taylor Smock
|
|---|
| 9 | * @since 18650
|
|---|
| 10 | */
|
|---|
| 11 | public interface IOAuthParameters {
|
|---|
| 12 | /**
|
|---|
| 13 | * Get the access token URL
|
|---|
| 14 | * @return The URL to use to switch the code to a token
|
|---|
| 15 | */
|
|---|
| 16 | String getAccessTokenUrl();
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Get the base authorization URL to open in a browser
|
|---|
| 20 | * @return The base URL to send to the browser
|
|---|
| 21 | */
|
|---|
| 22 | String getAuthorizationUrl();
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Get the authorization URL to open in a browser
|
|---|
| 26 | * @param state The state to prevent attackers from providing their own token
|
|---|
| 27 | * @param scopes The scopes to request
|
|---|
| 28 | * @return The URL to send to the browser
|
|---|
| 29 | */
|
|---|
| 30 | default String getAuthorizationUrl(String state, Enum<?>... scopes) {
|
|---|
| 31 | return this.getAuthorizationUrl(state, Stream.of(scopes).map(Enum::toString).toArray(String[]::new));
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Get the authorization URL to open in a browser
|
|---|
| 36 | * @param state The state to prevent attackers from providing their own token
|
|---|
| 37 | * @param scopes The scopes to request
|
|---|
| 38 | * @return The URL to send to the browser
|
|---|
| 39 | */
|
|---|
| 40 | default String getAuthorizationUrl(String state, String... scopes) {
|
|---|
| 41 | // response_type = code | token, but token is deprecated in the draft oauth 2.1 spec
|
|---|
| 42 | // 2.1 is adding code_challenge, code_challenge_method
|
|---|
| 43 | // code_challenge requires a code_verifier
|
|---|
| 44 | return this.getAuthorizationUrl() + "?response_type=code&client_id=" + this.getClientId()
|
|---|
| 45 | + "&redirect_uri=" + this.getRedirectUri()
|
|---|
| 46 | + "&scope=" + String.join(" ", scopes)
|
|---|
| 47 | // State is used to detect/prevent cross-site request forgery
|
|---|
| 48 | + "&state=" + state;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Get the OAuth version that the API expects
|
|---|
| 53 | * @return The oauth version
|
|---|
| 54 | */
|
|---|
| 55 | OAuthVersion getOAuthVersion();
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Get the client id
|
|---|
| 59 | * @return The client id
|
|---|
| 60 | */
|
|---|
| 61 | String getClientId();
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Get the client secret
|
|---|
| 65 | * @return The client secret
|
|---|
| 66 | */
|
|---|
| 67 | String getClientSecret();
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Get the redirect URI
|
|---|
| 71 | * @return The redirect URI
|
|---|
| 72 | */
|
|---|
| 73 | default String getRedirectUri() {
|
|---|
| 74 | return null;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Convert to a preference string
|
|---|
| 79 | * @return the preference string
|
|---|
| 80 | */
|
|---|
| 81 | default String toPreferencesString() {
|
|---|
| 82 | return null;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Get the actual API URL
|
|---|
| 87 | * @return The API URl
|
|---|
| 88 | */
|
|---|
| 89 | default String getApiUrl() {
|
|---|
| 90 | return null;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Store the preferences for these parameters
|
|---|
| 95 | */
|
|---|
| 96 | void rememberPreferences();
|
|---|
| 97 | }
|
|---|