Skip to content

Commit 1874352

Browse files
committed
feat: Implemented new authentication framework design
1 parent a084ad8 commit 1874352

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2616
-1965
lines changed

build/checkstyle-suppressions.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"http://checkstyle.sourceforge.net/dtds/suppressions_1_1.dtd">
66

77
<suppressions>
8-
<suppress checks="MemberName" files="ICP4DTokenResponse.java"/>
9-
<suppress checks="MethodName" files="ICP4DTokenResponse.java"/>
8+
<suppress checks="MemberName" files="Cp4dTokenResponse.java"/>
9+
<suppress checks="MethodName" files="Cp4dTokenResponse.java"/>
1010
<suppress checks="WhitespaceAround" files="JsonWebToken.java"/>
1111
<suppress checks="LineLength" files="JsonWebTokenTest.java"/>
12+
<suppress checks="HideUtilityClassConstructor" files="AuthenticatorBase.java"/>
1213
</suppressions>

ibm-credentials.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=123456789
2-
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
2+
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
3+
NATURAL_LANGUAGE_CLASSIFIER_DISABLE_SSL=true

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
<commons-io-version>2.6</commons-io-version>
5151
<commons-lang3-version>3.8.1</commons-lang3-version>
5252
<rx-version>2.2.7</rx-version>
53+
<mockito-version>2.0.5-beta</mockito-version>
54+
<powermock-version>1.6.2</powermock-version>
5355
</properties>
5456

5557
<!-- we use the travis providers for deployment so don't need to specify repositories here -->
@@ -140,6 +142,23 @@
140142
<artifactId>rxjava</artifactId>
141143
<version>${rx-version}</version>
142144
</dependency>
145+
<dependency>
146+
<groupId>org.mockito</groupId>
147+
<artifactId>mockito-core</artifactId>
148+
<version>${mockito-version}</version>
149+
</dependency>
150+
<dependency>
151+
<groupId>org.powermock</groupId>
152+
<artifactId>powermock-api-mockito</artifactId>
153+
<version>${powermock-version}</version>
154+
<scope>test</scope>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.powermock</groupId>
158+
<artifactId>powermock-module-junit4</artifactId>
159+
<version>${powermock-version}</version>
160+
<scope>test</scope>
161+
</dependency>
143162
</dependencies>
144163

145164
<build>

src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private void addParam(List<NameValue> params, String name, Object value) {
205205
}
206206

207207
/**
208-
* Builds the.
208+
* Builds the request.
209209
*
210210
* @return the request
211211
*/

src/main/java/com/ibm/cloud/sdk/core/security/noauth/NoauthConfig.java renamed to src/main/java/com/ibm/cloud/sdk/core/security/AbstractToken.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,12 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13-
package com.ibm.cloud.sdk.core.security.noauth;
14-
15-
import com.ibm.cloud.sdk.core.security.Authenticator;
16-
import com.ibm.cloud.sdk.core.security.AuthenticatorConfig;
13+
package com.ibm.cloud.sdk.core.security;
1714

1815
/**
19-
* This AuthenticatorConfig subclass is used in situations where we want to bypass authentication.
16+
* This class defines a common base class for tokens returned by a token server.
2017
*/
21-
public class NoauthConfig implements AuthenticatorConfig {
22-
23-
public NoauthConfig() {
24-
}
25-
26-
@Override
27-
public String authenticationType() {
28-
return Authenticator.AUTHTYPE_NOAUTH;
29-
}
30-
31-
@Override
32-
public void validate() {
33-
}
34-
18+
public abstract class AbstractToken {
19+
public abstract boolean isTokenValid();
20+
public abstract String getAccessToken();
3521
}

src/main/java/com/ibm/cloud/sdk/core/security/Authenticator.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import okhttp3.Request.Builder;
1616

1717
/**
18-
* This interface defines the common methods associated with an Authenticator implementation.
18+
* This interface defines the common methods and constants associated with an Authenticator implementation.
1919
*/
2020
public interface Authenticator {
2121

@@ -25,7 +25,26 @@ public interface Authenticator {
2525
String AUTHTYPE_BASIC = "basic";
2626
String AUTHTYPE_NOAUTH = "noauth";
2727
String AUTHTYPE_IAM = "iam";
28-
String AUTHTYPE_ICP4D = "icp4d";
28+
String AUTHTYPE_CP4D = "cp4d";
29+
String AUTHTYPE_BEARER_TOKEN = "bearerToken";
30+
31+
/**
32+
* Constants which define the names of external config propreties (credential file, environment variable, etc.).
33+
*/
34+
String PROPNAME_AUTH_TYPE = "AUTH_TYPE";
35+
String PROPNAME_USERNAME = "USERNAME";
36+
String PROPNAME_PASSWORD = "PASSWORD";
37+
String PROPNAME_BEARER_TOKEN = "BEARER_TOKEN";
38+
String PROPNAME_URL = "AUTH_URL";
39+
String PROPNAME_DISABLE_SSL = "AUTH_DISABLE_SSL";
40+
String PROPNAME_APIKEY = "APIKEY";
41+
String PROPNAME_CLIENT_ID = "CLIENT_ID";
42+
String PROPNAME_CLIENT_SECRET = "CLIENT_SECRET";
43+
44+
/**
45+
* Validates the current set of configuration information in the Authenticator.
46+
*/
47+
void validate();
2948

3049
/**
3150
* Returns the authentication type associated with the Authenticator instance.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.cloud.sdk.core.security;
14+
15+
import org.apache.commons.lang3.StringUtils;
16+
17+
import com.google.common.io.BaseEncoding;
18+
19+
/**
20+
* This is a common base class used with the various Authenticator implementations.
21+
*/
22+
public class AuthenticatorBase {
23+
24+
/**
25+
* Common error messages.
26+
*/
27+
public static final String ERRORMSG_PROP_MISSING = "The %s property is required but was not specified.";
28+
public static final String ERRORMSG_PROP_INVALID =
29+
"The %s property is invalid. Please remove any surrounding {, }, or \" characters.";
30+
public static final String ERRORMSG_REQ_FAILED = "Error while fetching access token from token service: ";
31+
32+
/**
33+
* Returns a "Basic" Authorization header value for the specified username and password.
34+
* @param username the username
35+
* @param password the password
36+
* @return the Authorization header value in the form "Basic &lt;encoded username and password&gt;"
37+
*/
38+
public static String constructBasicAuthHeader(String username, String password) {
39+
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
40+
return "Basic " + BaseEncoding.base64().encode(((username + ":" + password).getBytes()));
41+
}
42+
return null;
43+
}
44+
45+
/**
46+
* Returns a "Bearer" Authorization header value for the specified bearer token.
47+
* @param bearerToken the token value to be included in the header
48+
* @return the Authorization header value in the form "Bearer &lt;bearerToken&gt;"
49+
*/
50+
public static String constructBearerTokenAuthHeader(String bearerToken) {
51+
if (StringUtils.isNotEmpty(bearerToken)) {
52+
return "Bearer " + bearerToken;
53+
}
54+
return null;
55+
}
56+
}

src/main/java/com/ibm/cloud/sdk/core/security/AuthenticatorConfig.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/com/ibm/cloud/sdk/core/security/AuthenticatorFactory.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)