Skip to content

Commit

Permalink
feat: added device code manager
Browse files Browse the repository at this point in the history
  • Loading branch information
valmoz committed Feb 28, 2024
1 parent 01b877f commit 073b576
Show file tree
Hide file tree
Showing 14 changed files with 917 additions and 262 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.gson.annotations.SerializedName;
import java.util.Objects;

public class OAuth2AuthorizationCodeError {
public class OAuth2Error {
@SerializedName(value = "error")
private String error;

Expand All @@ -13,7 +13,7 @@ public class OAuth2AuthorizationCodeError {
@SerializedName(value = "code")
private Integer code;

public OAuth2AuthorizationCodeError(String error, String errorDescription, Integer code) {
public OAuth2Error(String error, String errorDescription, Integer code) {
this.error = error;
this.errorDescription = errorDescription;
this.code = code;
Expand Down Expand Up @@ -47,7 +47,7 @@ public void setCode(Integer code) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OAuth2AuthorizationCodeError that = (OAuth2AuthorizationCodeError) o;
OAuth2Error that = (OAuth2Error) o;
return Objects.equals(error, that.error)
&& Objects.equals(errorDescription, that.errorDescription)
&& Objects.equals(code, that.code);
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/it/fattureincloud/sdk/auth/OAuth2Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package it.fattureincloud.sdk.auth;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.vavr.control.Either;
import okhttp3.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public abstract class OAuth2Manager {
static final String DEFAULT_BASE_URI = "https://api-v2.fattureincloud.it";

protected String clientId;
protected Optional<String> baseUri;
protected OkHttpClient httpClient;
protected Gson gson;

protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

public OAuth2Manager(String clientId) {
this(clientId, null, null);
}

public OAuth2Manager(
String clientId, String baseUri) {
this(clientId, baseUri, null);
}

public OAuth2Manager(
String clientId, OkHttpClient httpClient) {
this(clientId, null, httpClient);
}

public OAuth2Manager(
String clientId,
String baseUri,
OkHttpClient httpClient) {
this.clientId = clientId;
this.baseUri = Optional.ofNullable(baseUri);
if (httpClient != null) {
this.httpClient = httpClient;
} else {
this.httpClient = new OkHttpClient();
}
this.gson = new Gson();
}

public String getClientId() {
return clientId;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getBaseUri() {
return baseUri.orElse(DEFAULT_BASE_URI);
}

public void setBaseUri(String baseUri) {
this.baseUri = Optional.ofNullable(baseUri);
}

public void setBaseUri(Optional<String> baseUri) {
this.baseUri = baseUri;
}

public OkHttpClient getHttpClient() {
return httpClient;
}

public void setHttpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
}

protected abstract Either<OAuth2Error, OAuth2TokenResponse> fetchToken(
String code) throws IOException;

protected abstract Either<OAuth2Error, OAuth2TokenResponse> refreshToken(
String refreshToken) throws IOException;

protected String getCompleteUri(String path) {
return this.getBaseUri().concat(path);
}

protected String mapToJson(Map<String, String> inputMap) {
return gson.toJson(inputMap);
}

protected <T> Either<OAuth2Error, T> post(
String url, String json, Class<T> typeOfT) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder().url(url).post(body).build();
try (Response response = this.httpClient.newCall(request).execute()) {
String responseBody = Objects.requireNonNull(response.body()).string();
int code = response.code();

if (code != 200) {
OAuth2Error err =
gson.fromJson(responseBody, OAuth2Error.class);
err.setCode(code);
return Either.left(err);
} else {
T res = gson.fromJson(responseBody, typeOfT);
return Either.right(res);
}
}
}

protected String encodeValue(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
return "";
}
}

protected String decodeValue(String value) {
try {
return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
return "";
}
}

protected String getScopesString(Collection<Scope> scopes) {
return scopes.stream().map(Scope::getScope).collect(Collectors.joining(" "));
}
}
Loading

0 comments on commit 073b576

Please sign in to comment.