Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
#37 Refactor AccessToken
Browse files Browse the repository at this point in the history
  • Loading branch information
proshin-roman committed Jan 19, 2019
1 parent 66d1687 commit 7160e86
Show file tree
Hide file tree
Showing 41 changed files with 272 additions and 207 deletions.
18 changes: 1 addition & 17 deletions src/main/java/org/proshin/finapi/accesstoken/AccessToken.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Roman Proshin
* Copyright 2019 Roman Proshin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,22 +15,6 @@
*/
package org.proshin.finapi.accesstoken;

import java.util.Optional;

/**
* @todo #14 Refactor AccessToken: leave accessToken only in this interface. Create two new interfaces
* UserAccessToken and ClientAccessToken with appropriate methods. Create new class SimpleAccessToken that receives
* a simple access token string. And finally get rid of FakeAccessToken.
*/
public interface AccessToken {

String accessToken();

String tokenType();

Optional<String> refreshToken();

int expiresIn();

String scope();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

public interface AccessTokens {

AccessToken clientToken(String clientId, String clientSecret);
ClientAccessToken clientToken(String clientId, String clientSecret);

AccessToken userToken(String clientId, String clientSecret, String username, String password);
UserAccessToken userToken(String clientId, String clientSecret, String username, String password);

AccessToken userToken(String clientId, String clientSecret, String refreshToken);
UserAccessToken userToken(String clientId, String clientSecret, String refreshToken);

void revoke(AccessToken clientToken, AccessToken userToken, RevokeToken tokensToRevoke);
void revoke(ClientAccessToken clientToken, UserAccessToken userToken, RevokeToken tokensToRevoke);

enum RevokeToken {
ACCESS_TOKEN_ONLY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,11 @@
*/
package org.proshin.finapi.accesstoken;

import java.util.Optional;
import org.json.JSONObject;
public interface ClientAccessToken extends AccessToken {

public final class ClientAccessToken implements AccessToken {
String tokenType();

private final JSONObject origin;
int expiresIn();

public ClientAccessToken(final JSONObject origin) {
this.origin = origin;
}

@Override
public String accessToken() {
return this.origin.getString("access_token");
}

@Override
public String tokenType() {
return this.origin.getString("token_type");
}

@Override
public Optional<String> refreshToken() {
return Optional.empty();
}

@Override
public int expiresIn() {
return this.origin.getInt("expires_in");
}

@Override
public String scope() {
return this.origin.getString("scope");
}
String scope();
}
18 changes: 11 additions & 7 deletions src/main/java/org/proshin/finapi/accesstoken/FpAccessTokens.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FpAccessTokens(final Endpoint endpoint, final String tokenUrl, final Stri
}

@Override
public AccessToken clientToken(final String clientId, final String clientSecret) {
public ClientAccessToken clientToken(final String clientId, final String clientSecret) {
final List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
parameters.add(new BasicNameValuePair("client_id", clientId));
Expand All @@ -53,15 +53,15 @@ public AccessToken clientToken(final String clientId, final String clientSecret)
} catch (final UnsupportedEncodingException exception) {
throw new RuntimeException("Couldn't instantiate an entity for POST request", exception);
}
return new ClientAccessToken(
return new FpClientAccessToken(
new JSONObject(
this.endpoint.post(this.tokenUrl, entity, 200)
)
);
}

@Override
public AccessToken userToken(
public UserAccessToken userToken(
final String clientId,
final String clientSecret,
final String username,
Expand All @@ -79,15 +79,15 @@ public AccessToken userToken(
} catch (final UnsupportedEncodingException exception) {
throw new RuntimeException("Couldn't instantiate an entity for POST request", exception);
}
return new UserAccessToken(
return new FpUserAccessToken(
new JSONObject(
this.endpoint.post(this.tokenUrl, entity, 200)
)
);
}

@Override
public AccessToken userToken(final String clientId, final String clientSecret, final String refreshToken) {
public UserAccessToken userToken(final String clientId, final String clientSecret, final String refreshToken) {
final List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new UrlEncodedPair("grant_type", "refresh_token"));
parameters.add(new UrlEncodedPair("client_id", clientId));
Expand All @@ -99,7 +99,7 @@ public AccessToken userToken(final String clientId, final String clientSecret, f
} catch (final UnsupportedEncodingException exception) {
throw new RuntimeException("Couldn't instantiate an entity for POST request", exception);
}
return new UserAccessToken(
return new FpUserAccessToken(
new JSONObject(
this.endpoint.post(this.tokenUrl, entity, 200)
)
Expand All @@ -115,7 +115,11 @@ public AccessToken userToken(final String clientId, final String clientSecret, f
* @todo #32 Test this method: assert that methods submits the right request
*/
@Override
public void revoke(final AccessToken clientToken, final AccessToken userToken, final RevokeToken tokensToRevoke) {
public void revoke(
final ClientAccessToken clientToken,
final UserAccessToken userToken,
final RevokeToken tokensToRevoke
) {
final List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("token", userToken.accessToken()));
switch (tokensToRevoke) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2018 Roman Proshin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.proshin.finapi.accesstoken;

import org.json.JSONObject;

public final class FpClientAccessToken implements ClientAccessToken {

private final JSONObject origin;

public FpClientAccessToken(final JSONObject origin) {
this.origin = origin;
}

@Override
public String accessToken() {
return this.origin.getString("access_token");
}

@Override
public String tokenType() {
return this.origin.getString("token_type");
}

@Override
public int expiresIn() {
return this.origin.getInt("expires_in");
}

@Override
public String scope() {
return this.origin.getString("scope");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Roman Proshin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.proshin.finapi.accesstoken;

import java.util.Optional;
import org.json.JSONObject;
import org.proshin.finapi.exception.NoFieldException;

public final class FpUserAccessToken implements UserAccessToken {

private final JSONObject origin;

public FpUserAccessToken(final JSONObject origin) {
this.origin = origin;
}

@Override
public String accessToken() {
return this.origin.getString("access_token");
}

@Override
public String tokenType() {
return this.origin.getString("token_type");
}

@Override
public Optional<String> refreshToken() {
final String name = "refresh_token";
if (this.origin.isNull(name)) {
throw new NoFieldException("Field 'refresh_token' may not be null for user's access token");
} else {
return Optional.of(this.origin.getString(name));
}
}

@Override
public int expiresIn() {
return this.origin.getInt("expires_in");
}

@Override
public String scope() {
return this.origin.getString("scope");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Roman Proshin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.proshin.finapi.accesstoken;

public class SimpleAccessToken implements AccessToken {

private final String token;

public SimpleAccessToken(final String token) {
this.token = token;
}

@Override
public String accessToken() {
return this.token;
}
}
40 changes: 5 additions & 35 deletions src/main/java/org/proshin/finapi/accesstoken/UserAccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,14 @@
package org.proshin.finapi.accesstoken;

import java.util.Optional;
import org.json.JSONObject;
import org.proshin.finapi.exception.NoFieldException;

public final class UserAccessToken implements AccessToken {
public interface UserAccessToken extends AccessToken {

private final JSONObject origin;
String tokenType();

public UserAccessToken(final JSONObject origin) {
this.origin = origin;
}
Optional<String> refreshToken();

@Override
public String accessToken() {
return this.origin.getString("access_token");
}
int expiresIn();

@Override
public String tokenType() {
return this.origin.getString("token_type");
}

@Override
public Optional<String> refreshToken() {
final String name = "refresh_token";
if (this.origin.isNull(name)) {
throw new NoFieldException("Field 'refresh_token' may not be null for user's access token");
} else {
return Optional.of(this.origin.getString(name));
}
}

@Override
public int expiresIn() {
return this.origin.getInt("expires_in");
}

@Override
public String scope() {
return this.origin.getString("scope");
}
String scope();
}
6 changes: 3 additions & 3 deletions src/main/java/org/proshin/finapi/account/FpAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.time.OffsetDateTime;
import java.util.Optional;
import org.json.JSONObject;
import org.proshin.finapi.accesstoken.AccessToken;
import org.proshin.finapi.accesstoken.UserAccessToken;
import org.proshin.finapi.account.in.FpEditParameters;
import org.proshin.finapi.account.out.ClearingAccount;
import org.proshin.finapi.account.out.FpClearingAccount;
Expand All @@ -42,11 +42,11 @@
public final class FpAccount implements Account {

private final Endpoint endpoint;
private final AccessToken token;
private final UserAccessToken token;
private final JSONObject origin;
private final String url;

public FpAccount(final Endpoint endpoint, final AccessToken token, final JSONObject origin, final String url) {
public FpAccount(final Endpoint endpoint, final UserAccessToken token, final JSONObject origin, final String url) {
this.endpoint = endpoint;
this.token = token;
this.origin = origin;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/proshin/finapi/account/FpAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.proshin.finapi.account;

import org.json.JSONObject;
import org.proshin.finapi.accesstoken.AccessToken;
import org.proshin.finapi.accesstoken.UserAccessToken;
import org.proshin.finapi.account.in.DailyBalancesCriteria;
import org.proshin.finapi.account.in.FpQueryCriteria;
import org.proshin.finapi.account.out.DailyBalances;
Expand All @@ -32,14 +32,14 @@
public final class FpAccounts implements Accounts {

private final Endpoint endpoint;
private final AccessToken token;
private final UserAccessToken token;
private final String url;

public FpAccounts(final Endpoint endpoint, final AccessToken token) {
public FpAccounts(final Endpoint endpoint, final UserAccessToken token) {
this(endpoint, token, "/api/v1/accounts/");
}

public FpAccounts(final Endpoint endpoint, final AccessToken token, final String url) {
public FpAccounts(final Endpoint endpoint, final UserAccessToken token, final String url) {
this.endpoint = endpoint;
this.token = token;
this.url = url;
Expand Down
Loading

0 comments on commit 7160e86

Please sign in to comment.