From 7b2d5b7685f92e55114117f9a654573934b45fcd Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Tue, 7 Jun 2016 21:10:19 -0700 Subject: [PATCH] sheetsu (#84) --- commands/sheetsuapi | 19 ++++++ src/main/java/com/baulsupp/oksocial/Main.java | 12 ++-- .../authenticator/BasicCredentials.java | 22 +++++++ .../sheetsu/SheetsuAuthInterceptor.java | 63 +++++++++++++++++++ .../sheetsu/SheetsuServiceDefinition.java | 25 ++++++++ .../services/sheetsu/SheetsuUtil.java | 15 +++++ .../services/uber/UberAuthInterceptor.java | 13 ++-- .../services/uber/UberServerCredentials.java | 21 ------- .../services/uber/UberServiceDefinition.java | 11 ++-- .../oksocial/util/WrappedRequestBody.java | 24 +++++++ ...upp.oksocial.authenticator.AuthInterceptor | 1 + 11 files changed, 188 insertions(+), 38 deletions(-) create mode 100755 commands/sheetsuapi create mode 100644 src/main/java/com/baulsupp/oksocial/authenticator/BasicCredentials.java create mode 100644 src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuAuthInterceptor.java create mode 100644 src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuServiceDefinition.java create mode 100644 src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuUtil.java delete mode 100644 src/main/java/com/baulsupp/oksocial/services/uber/UberServerCredentials.java create mode 100644 src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java diff --git a/commands/sheetsuapi b/commands/sheetsuapi new file mode 100755 index 00000000..f6e16a36 --- /dev/null +++ b/commands/sheetsuapi @@ -0,0 +1,19 @@ +#!/usr/bin/env okapi -m + +var sheet = arguments[0]; + +var request = requestBuilder + .url("https://sheetsu.com/apis/v1.0/" + sheet) + .build(); + +if (request.body()) { + requestBuilder = request.newBuilder(); + + var BodyWrapper = Java.type("com.baulsupp.oksocial.util.WrappedRequestBody"); + var body = new BodyWrapper(request.body(), "application/json"); + + request = requestBuilder.method(request.method(), body).build(); +} + +request + diff --git a/src/main/java/com/baulsupp/oksocial/Main.java b/src/main/java/com/baulsupp/oksocial/Main.java index 322f7a55..384d29e3 100644 --- a/src/main/java/com/baulsupp/oksocial/Main.java +++ b/src/main/java/com/baulsupp/oksocial/Main.java @@ -522,22 +522,22 @@ private RequestBody getRequestBody() { } public Request.Builder createRequestBuilder() { - Request.Builder request = new Request.Builder(); + Request.Builder requestBuilder = new Request.Builder(); - request.method(getRequestMethod(), getRequestBody()); + requestBuilder.method(getRequestMethod(), getRequestBody()); if (headers != null) { for (String header : headers) { String[] parts = header.split(":", 2); - request.header(parts[0], parts[1]); + requestBuilder.header(parts[0], parts[1]); } } if (referer != null) { - request.header("Referer", referer); + requestBuilder.header("Referer", referer); } - request.header("User-Agent", userAgent); + requestBuilder.header("User-Agent", userAgent); - return request; + return requestBuilder; } private static SSLSocketFactory createSslSocketFactory(KeyManager[] keyManagers, diff --git a/src/main/java/com/baulsupp/oksocial/authenticator/BasicCredentials.java b/src/main/java/com/baulsupp/oksocial/authenticator/BasicCredentials.java new file mode 100644 index 00000000..aa092fba --- /dev/null +++ b/src/main/java/com/baulsupp/oksocial/authenticator/BasicCredentials.java @@ -0,0 +1,22 @@ +package com.baulsupp.oksocial.authenticator; + +public class BasicCredentials { + public String user; + public String password; + + public BasicCredentials() { + } + + public BasicCredentials(String user, String password) { + this.user = user; + this.password = password; + } + + @Override + public String toString() { + return "BasicCredentials{" + + "user='" + user + '\'' + + "password='" + password + '\'' + + '}'; + } +} diff --git a/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuAuthInterceptor.java b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuAuthInterceptor.java new file mode 100644 index 00000000..85bc088a --- /dev/null +++ b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuAuthInterceptor.java @@ -0,0 +1,63 @@ +package com.baulsupp.oksocial.services.sheetsu; + +import com.baulsupp.oksocial.authenticator.AuthInterceptor; +import com.baulsupp.oksocial.authenticator.BasicCredentials; +import com.baulsupp.oksocial.credentials.CredentialsStore; +import com.baulsupp.oksocial.secrets.Secrets; +import java.io.IOException; +import java.util.Optional; +import okhttp3.Credentials; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class SheetsuAuthInterceptor implements AuthInterceptor { + private final CredentialsStore credentialsStore = + CredentialsStore.create(new SheetsuServiceDefinition()); + public static final String NAME = "sheetsu"; + + @Override public String name() { + return NAME; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + Optional credentials = readCredentials(); + if (credentials.isPresent()) { + BasicCredentials token = readCredentials().get(); + + request = + request.newBuilder() + .addHeader("Authorization", Credentials.basic(token.user, token.password)) + .build(); + } + + return chain.proceed(request); + } + + @Override + public CredentialsStore credentialsStore() { + return credentialsStore; + } + + public boolean supportsUrl(HttpUrl url) { + String host = url.host(); + + return SheetsuUtil.API_HOSTS.contains(host); + } + + @Override + public void authorize(OkHttpClient client) { + String user = + Secrets.prompt("Sheetsu API Key", "sheetsu.apiKey", "", false); + String password = + Secrets.prompt("Sheetsu API Password", "sheetsu.apiSecret", "", true); + + BasicCredentials newCredentials = new BasicCredentials(user, password); + + credentialsStore.storeCredentials(newCredentials); + } +} diff --git a/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuServiceDefinition.java b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuServiceDefinition.java new file mode 100644 index 00000000..18a21381 --- /dev/null +++ b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuServiceDefinition.java @@ -0,0 +1,25 @@ +package com.baulsupp.oksocial.services.sheetsu; + +import com.baulsupp.oksocial.authenticator.BasicCredentials; +import com.baulsupp.oksocial.credentials.ServiceDefinition; + +public class SheetsuServiceDefinition implements ServiceDefinition { + @Override + public String apiHost() { + return "sheetsu.com"; + } + + @Override + public String serviceName() { + return "Sheetsu API"; + } + + public BasicCredentials parseCredentialsString(String s) { + String[] parts = s.split(":", 2); + return new BasicCredentials(parts[0], parts[1]); + } + + public String formatCredentialsString(BasicCredentials credentials) { + return credentials.user + ":" + credentials.password; + } +} diff --git a/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuUtil.java b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuUtil.java new file mode 100644 index 00000000..508c4e17 --- /dev/null +++ b/src/main/java/com/baulsupp/oksocial/services/sheetsu/SheetsuUtil.java @@ -0,0 +1,15 @@ +package com.baulsupp.oksocial.services.sheetsu; + +import com.google.common.collect.Sets; +import java.util.Collections; +import java.util.Set; + +public class SheetsuUtil { + private SheetsuUtil() { + } + + public static final Set API_HOSTS = + Collections.unmodifiableSet(Sets.newHashSet( + "sheetsu.com") + ); +} diff --git a/src/main/java/com/baulsupp/oksocial/services/uber/UberAuthInterceptor.java b/src/main/java/com/baulsupp/oksocial/services/uber/UberAuthInterceptor.java index 517670cb..c3d3001e 100644 --- a/src/main/java/com/baulsupp/oksocial/services/uber/UberAuthInterceptor.java +++ b/src/main/java/com/baulsupp/oksocial/services/uber/UberAuthInterceptor.java @@ -1,6 +1,7 @@ package com.baulsupp.oksocial.services.uber; import com.baulsupp.oksocial.authenticator.AuthInterceptor; +import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token; import com.baulsupp.oksocial.credentials.CredentialsStore; import com.baulsupp.oksocial.secrets.Secrets; import java.io.IOException; @@ -11,8 +12,8 @@ import okhttp3.Request; import okhttp3.Response; -public class UberAuthInterceptor implements AuthInterceptor { - private final CredentialsStore credentialsStore = +public class UberAuthInterceptor implements AuthInterceptor { + private final CredentialsStore credentialsStore = CredentialsStore.create(new UberServiceDefinition()); public static final String NAME = "uber"; @@ -24,9 +25,9 @@ public class UberAuthInterceptor implements AuthInterceptor credentials = readCredentials(); + Optional credentials = readCredentials(); if (credentials.isPresent()) { - String token = readCredentials().get().serverToken; + String token = readCredentials().get().accessToken; request = request.newBuilder().addHeader("Authorization", "Token " + token).build(); @@ -36,7 +37,7 @@ public Response intercept(Interceptor.Chain chain) throws IOException { } @Override - public CredentialsStore credentialsStore() { + public CredentialsStore credentialsStore() { return credentialsStore; } @@ -51,7 +52,7 @@ public void authorize(OkHttpClient client) { String serverToken = Secrets.prompt("Uber Server Token", "uber.serverToken", "", true); - UberServerCredentials newCredentials = new UberServerCredentials(serverToken); + Oauth2Token newCredentials = new Oauth2Token(serverToken); credentialsStore.storeCredentials(newCredentials); } diff --git a/src/main/java/com/baulsupp/oksocial/services/uber/UberServerCredentials.java b/src/main/java/com/baulsupp/oksocial/services/uber/UberServerCredentials.java deleted file mode 100644 index d24aaf35..00000000 --- a/src/main/java/com/baulsupp/oksocial/services/uber/UberServerCredentials.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baulsupp.oksocial.services.uber; - -public class UberServerCredentials { - public String serverToken; - - public UberServerCredentials() { - } - - public UberServerCredentials(String serverToken) { - this.serverToken = serverToken; - } - - @Override - public String toString() { - return "UberServerCredentials{" - + "serverToken='" - + serverToken - + '\'' - + '}'; - } -} diff --git a/src/main/java/com/baulsupp/oksocial/services/uber/UberServiceDefinition.java b/src/main/java/com/baulsupp/oksocial/services/uber/UberServiceDefinition.java index c73d5410..642534d1 100644 --- a/src/main/java/com/baulsupp/oksocial/services/uber/UberServiceDefinition.java +++ b/src/main/java/com/baulsupp/oksocial/services/uber/UberServiceDefinition.java @@ -1,8 +1,9 @@ package com.baulsupp.oksocial.services.uber; +import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token; import com.baulsupp.oksocial.credentials.ServiceDefinition; -public class UberServiceDefinition implements ServiceDefinition { +public class UberServiceDefinition implements ServiceDefinition { @Override public String apiHost() { return "api.uber.com"; @@ -13,11 +14,11 @@ public String serviceName() { return "Uber API"; } - public UberServerCredentials parseCredentialsString(String s) { - return new UberServerCredentials(s); + public Oauth2Token parseCredentialsString(String s) { + return new Oauth2Token(s); } - public String formatCredentialsString(UberServerCredentials credentials) { - return credentials.serverToken; + public String formatCredentialsString(Oauth2Token credentials) { + return credentials.accessToken; } } diff --git a/src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java b/src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java new file mode 100644 index 00000000..3a629d99 --- /dev/null +++ b/src/main/java/com/baulsupp/oksocial/util/WrappedRequestBody.java @@ -0,0 +1,24 @@ +package com.baulsupp.oksocial.util; + +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okio.BufferedSink; + +public class WrappedRequestBody extends RequestBody { + private final String contentType; + private final RequestBody body; + + public WrappedRequestBody(RequestBody body, String contentType) { + this.body = body; + this.contentType = contentType; + } + + @Override public MediaType contentType() { + return MediaType.parse(contentType); + } + + @Override public void writeTo(BufferedSink bufferedSink) throws IOException { + body.writeTo(bufferedSink); + } +} diff --git a/src/main/resources/META-INF/services/com.baulsupp.oksocial.authenticator.AuthInterceptor b/src/main/resources/META-INF/services/com.baulsupp.oksocial.authenticator.AuthInterceptor index 252c6a37..f21cedea 100644 --- a/src/main/resources/META-INF/services/com.baulsupp.oksocial.authenticator.AuthInterceptor +++ b/src/main/resources/META-INF/services/com.baulsupp.oksocial.authenticator.AuthInterceptor @@ -3,6 +3,7 @@ com.baulsupp.oksocial.services.foursquare.FourSquareAuthInterceptor com.baulsupp.oksocial.services.google.GoogleAuthInterceptor com.baulsupp.oksocial.services.imgur.ImgurAuthInterceptor com.baulsupp.oksocial.services.lyft.LyftAuthInterceptor +com.baulsupp.oksocial.services.sheetsu.SheetsuAuthInterceptor com.baulsupp.oksocial.services.squareup.SquareUpAuthInterceptor com.baulsupp.oksocial.services.stackexchange.StackExchangeAuthInterceptor com.baulsupp.oksocial.services.twitter.TwitterAuthInterceptor