-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Carl-Tucker/token_branch
Added support for BP 5.4.2 JWT Tokens
- Loading branch information
Showing
5 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
bp-mgmt-client/src/main/java/com/spectralogic/blackpearl/management/models/Token.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
bp-mgmt-client/src/main/java/com/spectralogic/blackpearl/management/models/UserCreds.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...client/src/main/java/com/spectralogic/blackpearl/management/network/TokenInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |