Commit dc245990 authored by Benjamin Beeker's avatar Benjamin Beeker

clientInformation with callback-url

parent 3f0306c0
Pipeline #10858006 passed with stages
in 1 minute and 42 seconds
......@@ -48,7 +48,16 @@ public class TwitchApi {
* You should keep the instance of {@link de.comhix.twitch.api.oauth.TwitchAuthenticator.SecondStep} somewhere cached to enter the auth-code.
*/
public static TwitchAuthenticator.SecondStep startOAuth(ClientInformation clientInformation, String callbackUrl) {
return new TwitchAuthenticator(clientInformation).doAuth(callbackUrl);
return new TwitchAuthenticator(clientInformation, callbackUrl).doAuth();
}
/**
* Starts the oAuth process. Best for Website-based bots with own callback-url.
* {@link TwitchAuthenticator.SecondStep#getTargetUrl()} provides the URL the user should be redirected to for the oAuth process.
* You should keep the instance of {@link de.comhix.twitch.api.oauth.TwitchAuthenticator.SecondStep} somewhere cached to enter the auth-code.
*/
public static TwitchAuthenticator.SecondStep startOAuth(ClientInformation clientInformation) {
return new TwitchAuthenticator(clientInformation).doAuth();
}
/**
......
package de.comhix.twitch.api.oauth;
import com.google.common.base.Preconditions;
import com.google.gson.annotations.SerializedName;
public class ClientInformation {
......@@ -10,9 +11,17 @@ public class ClientInformation {
@SerializedName("client-secret")
private final String clientSecret;
@SerializedName("callback-url")
private final String callbackUrl;
public ClientInformation(String clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this(clientId, clientSecret, null);
}
public ClientInformation(String clientId, String clientSecret, String callbackUrl) {
this.clientId = Preconditions.checkNotNull(clientId,"clientId may not be null");
this.clientSecret = Preconditions.checkNotNull(clientSecret,"clientSecret may not be null");
this.callbackUrl = callbackUrl;
}
public String getClientId() {
......@@ -23,11 +32,16 @@ public class ClientInformation {
return clientSecret;
}
public String getCallbackUrl() {
return callbackUrl;
}
@Override
public String toString() {
return "ClientInformation{" +
"clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
", callbackUrl='" + callbackUrl + '\'' +
'}';
}
}
......@@ -38,10 +38,10 @@ public class SelfhostedTwitchAuthenticator {
}
public Observable<OAuthResponse> doOAuth() {
TwitchAuthenticator twitchAuthenticator = new TwitchAuthenticator(clientInformation);
String callbackUrl = String.format(REDIRECT_URI, port);
TwitchAuthenticator.SecondStep secondStep = twitchAuthenticator.doAuth(callbackUrl);
TwitchAuthenticator twitchAuthenticator = new TwitchAuthenticator(clientInformation,callbackUrl);
TwitchAuthenticator.SecondStep secondStep = twitchAuthenticator.doAuth();
try {
Desktop.getDesktop().browse(new URI(secondStep.getTargetUrl()));
......
......@@ -21,38 +21,41 @@ import static com.google.common.collect.Maps.newHashMap;
* @author Benjamin Beeker
*/
public class TwitchAuthenticator {
private final ClientInformation clientInformation;
private static final ImmutableList<String> SCOPES = ImmutableList.of("chat_login",
"user_blocks_edit",
"channel_editor",
"channel_feed_edit",
"user_read");
private final ClientInformation clientInformation;
public TwitchAuthenticator(ClientInformation clientInformation) {
this.clientInformation = Preconditions.checkNotNull(clientInformation);
this.clientInformation = Preconditions.checkNotNull(clientInformation, "clientInformation may not be null");
Preconditions.checkNotNull(clientInformation.getCallbackUrl(), "callbackUrl may not be null");
}
public TwitchAuthenticator(ClientInformation clientInformation, String callbackUrl) {
this(new ClientInformation(clientInformation.getClientId(), clientInformation.getClientSecret(), callbackUrl));
}
public SecondStep doAuth(String callbackUrl) {
public SecondStep doAuth() {
String state = RandomStringUtils.randomAlphabetic(8);
String targetUrl = "https://api.twitch.tv/kraken/oauth2/authorize" +
"?response_type=code" +
"&client_id=" + clientInformation.getClientId() +
"&redirect_uri=" + callbackUrl +
"&redirect_uri=" + clientInformation.getCallbackUrl() +
"&scope=" + Joiner.on("+").join(SCOPES) +
"&state=" + state;
return new SecondStep(state, callbackUrl, targetUrl);
return new SecondStep(state, targetUrl);
}
public class SecondStep {
private final String state;
private final String callbackUrl;
private final String targetUrl;
SecondStep(String state, String callbackUrl, String targetUrl) {
SecondStep(String state, String targetUrl) {
this.state = state;
this.callbackUrl = callbackUrl;
this.targetUrl = targetUrl;
}
......@@ -70,7 +73,7 @@ public class TwitchAuthenticator {
postData.put("client_id", clientInformation.getClientId());
postData.put("client_secret", clientInformation.getClientSecret());
postData.put("grant_type", "authorization_code");
postData.put("redirect_uri", callbackUrl);
postData.put("redirect_uri", clientInformation.getCallbackUrl());
postData.put("code", authCode);
postData.put("state", state);
String postString = Joiner.on("&").withKeyValueSeparator("=").join(postData);
......
package de.comhix.twitch.api.oauth;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Benjamin Beeker
*/
public class ClientInformationTest {
private static final Logger log = LoggerFactory.getLogger(ClientInformationTest.class);
@Test
public void testLoadFromJson() throws Exception {
log.info("testLoadFromJson");
String json="{\n" +
" \"client-id\": \"datId\",\n" +
" \"client-secret\": \"datSecret\",\n" +
" \"callback-url\": \"http://localhost:8080/oauth/callback\"\n" +
"}";
ClientInformation information = new Gson().fromJson(json, ClientInformation.class);
assertThat(information).isNotNull();
assertThat(information.getClientId()).isEqualTo("datId");
assertThat(information.getClientSecret()).isEqualTo("datSecret");
assertThat(information.getCallbackUrl()).isEqualTo("http://localhost:8080/oauth/callback");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment