Skip to content

Commit

Permalink
Merge pull request #6 from Carl-Tucker/token_branch
Browse files Browse the repository at this point in the history
Added support for BP 5.4.2 JWT Tokens
  • Loading branch information
david-limbach authored Feb 8, 2022
2 parents ddf2bd7 + fbac650 commit 0c025a3
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public interface BlackPearlManagementService {
@POST("logs")
Single<LogCreateResponse> createLogSet(@Body final LogCreator logCreator);

@POST("tokens")
Single<Token> generateToken(@Body final UserCreds credentials);

@GET("logs/{logSetName}")
@Streaming
Single<ResponseBody> getLogSet(@Path("logSetName") final String logSetName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* ****************************************************************************
* Copyright 2016 Spectra Logic Corporation. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
* or in the "license" file accompanying this file.
* This file 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 com.spectralogic.blackpearl.management.models;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Token {
@JsonProperty("token")
private String token;

public String getToken() {
return token;
}

public void setToken(final String token) {
this.token = token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.spectralogic.blackpearl.management.models;

import com.fasterxml.jackson.annotation.JsonProperty;

public class UserCreds {
@JsonProperty("username")
private String username;

@JsonProperty("password")
private String password;

public String getUsername() {
return username;
}

public void setUsername(final String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(final String password) {
this.password = password;
}

public void setUserAndPass(final String user, final String pass) {this.username = user; this.password = pass;}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,12 @@ public static Retrofit build(final String url, final String username, final Stri
}

public static Retrofit build(final String url, final String username, final String password, final boolean apiPrefix) throws KeyManagementException, NoSuchAlgorithmException {
final String authorization = "Basic " + Base64
.getEncoder()
.encodeToString((username + ":" + password).getBytes(Charset.forName("UTF-8")));
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new TokenInterceptor(url, username, password));
httpClient.addInterceptor(chain -> {
final Request original = chain.request();

final Request request = original.newBuilder()
.header("Authorization", authorization)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.method(original.method(), original.body())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* ****************************************************************************
* Copyright 2016 Spectra Logic Corporation. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
* or in the "license" file accompanying this file.
* This file 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 com.spectralogic.blackpearl.management.network;

import com.spectralogic.blackpearl.management.BlackPearlManagementService;
import com.spectralogic.blackpearl.management.models.Token;
import com.spectralogic.blackpearl.management.models.UserCreds;
import io.reactivex.Single;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

class TokenInterceptor implements Interceptor {
private final String tokenUrl;
private final String user;
private final String pass;
private final static String API_TOKENS = "/api/tokens";

public TokenInterceptor(String url, String username, String password) {
tokenUrl = url;
user = username;
pass = password;
}

@Override public Response intercept(Chain chain) throws IOException {
final Request original = chain.request();

if (original.url().encodedPath().contains(API_TOKENS) && original.method().equals("POST")) {
return chain.proceed(original);
}
try {
final String token = fetchToken();
final String authorization = "Bearer " + token;
final Request request = original.newBuilder()
.header("Authorization", authorization)
.method(original.method(), original.body())
.build();
return chain.proceed(request);

} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("Failed to Fetch JWT Token", e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("Failed to Fetch JWT Token", e);
} catch (KeyManagementException e) {
e.printStackTrace();
throw new RuntimeException("Failed to Fetch JWT Token", e);
}
}

private String fetchToken() throws InterruptedException, NoSuchAlgorithmException, KeyManagementException {
final String tokensEndpoint = tokenUrl;

BlackPearlManagementService managementService = BlackPearlManagementService.getInstance(tokensEndpoint, user, pass);

UserCreds userCreds = new UserCreds();
userCreds.setUserAndPass(user, pass);

final Single<Token> token = managementService.generateToken(userCreds);

return token.map(Token::getToken).blockingGet();
}
}

0 comments on commit 0c025a3

Please sign in to comment.