-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more authentication methods.
- Loading branch information
Showing
13 changed files
with
234 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ plugins { | |
} | ||
|
||
group 'com.notronix' | ||
version '3.0.0011' | ||
version '3.0.0012' | ||
|
||
sourceCompatibility = 1.8 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/notronix/etsy/api/authentication/method/GetAccessTokenMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.notronix.etsy.api.authentication.method; | ||
|
||
import com.notronix.etsy.api.AppKey; | ||
import com.notronix.etsy.api.authentication.model.TokenResponse; | ||
import com.notronix.etsy.api.common.method.Method; | ||
|
||
public interface GetAccessTokenMethod<C> extends Method<TokenResponse, C> | ||
{ | ||
void setAppKey(AppKey appKey); | ||
void setRedirectURI(String redirectURI); | ||
void setCode(String code); | ||
void setCodeVerifier(String codeVerifier); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
public enum GrantType | ||
{ | ||
authorization_code, | ||
refresh_token, | ||
token_exchange | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/notronix/etsy/api/authentication/model/OAuthConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.notronix.etsy.api.authentication.model; | ||
|
||
import com.notronix.etsy.api.AppKey; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.notronix.etsy.api.MethodUtils.addIfProvided; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public interface OAuthConnector | ||
{ | ||
String BASE_URL = "https://www.etsy.com/oauth/connect"; | ||
|
||
Function<String, String> urlEncoder = s -> { | ||
try { | ||
return URLEncoder.encode(requireNonNull(s), StandardCharsets.UTF_8.name()) | ||
.replace("+", "-") | ||
.replace("/", "_") | ||
.replace("=", ""); | ||
} | ||
catch (UnsupportedEncodingException ex) { | ||
throw new IllegalArgumentException("url encoding error", ex); | ||
} | ||
}; | ||
|
||
String getState(); | ||
|
||
String getCodeVerifier(); | ||
|
||
String getCodeChallenge(); | ||
|
||
String getConnectionURL(AppKey appKey, String redirectURL, Set<EtsyScope> scopes); | ||
|
||
static OAuthConnector withValues(String state, String verifier, String challenge) { | ||
return new OAuthConnector() | ||
{ | ||
@Override | ||
public String getState() { | ||
return state; | ||
} | ||
|
||
@Override | ||
public String getCodeVerifier() { | ||
return verifier; | ||
} | ||
|
||
@Override | ||
public String getCodeChallenge() { | ||
return challenge; | ||
} | ||
|
||
@Override | ||
public String getConnectionURL(AppKey appKey, String redirectURL, Set<EtsyScope> scopes) { | ||
String url = addIfProvided(BASE_URL, "response_type", "code"); | ||
url = addIfProvided(url, "client_id", requireNonNull(appKey.getValue())); | ||
url = addIfProvided(url, "redirect_uri", urlEncoder.apply(requireNonNull(redirectURL))); | ||
url = addIfProvided(url, "scope", requireNonNull(scopes).stream().map(Enum::name) | ||
.collect(Collectors.joining("%20"))); | ||
url = addIfProvided(url, "state", state); | ||
url = addIfProvided(url, "code_challenge", challenge); | ||
url = addIfProvided(url, "code_challenge_method", "S256"); | ||
|
||
return url; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/com/notronix/etsy/impl/authentication/method/EtsyGetAccessTokenMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.notronix.etsy.impl.authentication.method; | ||
|
||
import com.google.api.client.http.HttpContent; | ||
import com.google.api.client.http.HttpMethods; | ||
import com.google.api.client.http.UrlEncodedContent; | ||
import com.notronix.etsy.api.AppKey; | ||
import com.notronix.etsy.api.Marshaller; | ||
import com.notronix.etsy.api.Unmarshaller; | ||
import com.notronix.etsy.api.authentication.method.GetAccessTokenMethod; | ||
import com.notronix.etsy.api.authentication.method.GrantType; | ||
import com.notronix.etsy.api.authentication.model.TokenResponse; | ||
import com.notronix.etsy.impl.AbstractEtsyMethod; | ||
import com.notronix.etsy.impl.authentication.model.EtsyTokenResponse; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class EtsyGetAccessTokenMethod extends AbstractEtsyMethod<TokenResponse> | ||
implements GetAccessTokenMethod<HttpContent> | ||
{ | ||
private AppKey appKey; | ||
private String redirectURI; | ||
private String code; | ||
private String codeVerifier; | ||
|
||
@Override | ||
public EtsyTokenResponse buildResponseBody(Unmarshaller unmarshaller, String payload) { | ||
return unmarshaller.unmarshal(payload, EtsyTokenResponse.class); | ||
} | ||
|
||
@Override | ||
protected String getURI() { | ||
return "/public/oauth/token"; | ||
} | ||
|
||
@Override | ||
public String getRequestMethod() { | ||
return HttpMethods.POST; | ||
} | ||
|
||
@Override | ||
public HttpContent buildRequestContent(Marshaller marshaller) { | ||
Map<String, Object> parameters = new HashMap<>(); | ||
parameters.put("grant_type", GrantType.authorization_code.name()); | ||
parameters.put("client_id", requireNonNull(appKey.getValue())); | ||
parameters.put("redirect_uri", requireNonNull(redirectURI)); | ||
parameters.put("code", requireNonNull(code)); | ||
parameters.put("code_verifier", requireNonNull(codeVerifier)); | ||
|
||
return new UrlEncodedContent(parameters); | ||
} | ||
|
||
public AppKey getAppKey() { | ||
return appKey; | ||
} | ||
|
||
@Override | ||
public void setAppKey(AppKey appKey) { | ||
this.appKey = appKey; | ||
} | ||
|
||
public EtsyGetAccessTokenMethod withAppKey(AppKey appKey) { | ||
this.appKey = appKey; | ||
return this; | ||
} | ||
|
||
public String getRedirectURI() { | ||
return redirectURI; | ||
} | ||
|
||
@Override | ||
public void setRedirectURI(String redirectURI) { | ||
this.redirectURI = redirectURI; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
@Override | ||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCodeVerifier() { | ||
return codeVerifier; | ||
} | ||
|
||
@Override | ||
public void setCodeVerifier(String codeVerifier) { | ||
this.codeVerifier = codeVerifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters