11package com .appirio .tech .core .service .identity .util .auth ;
22
3- import java .net .HttpURLConnection ;
4- import java .util .Date ;
5-
63import javax .validation .constraints .NotNull ;
74
8- import org .apache .log4j .Logger ;
9-
10- import com .appirio .tech .core .api .v3 .exception .APIRuntimeException ;
11- import com .appirio .tech .core .api .v3 .util .jwt .InvalidTokenException ;
12- import com .appirio .tech .core .service .identity .util .HttpUtil .Request ;
13- import com .appirio .tech .core .service .identity .util .HttpUtil .Response ;
14- import com .auth0 .jwt .JWT ;
15- import com .auth0 .jwt .exceptions .JWTDecodeException ;
16- import com .auth0 .jwt .interfaces .DecodedJWT ;
17- import com .fasterxml .jackson .databind .ObjectMapper ;
18-
195public class DICEAuth {
20- private static final Logger logger = Logger .getLogger (Auth0Client .class );
216
227 @ NotNull
238 private String diceUrl ;
@@ -29,44 +14,21 @@ public class DICEAuth {
2914 private String diceVerifier ;
3015
3116 @ NotNull
32- private String tenant ;
33-
34- @ NotNull
35- private String username ;
36-
37- @ NotNull
38- private String password ;
39-
40- @ NotNull
41- private String scope ;
42-
43- @ NotNull
44- private String clientId ;
45-
46- @ NotNull
47- private String clientSecret ;
17+ private String diceApiKey ;
4818
4919 @ NotNull
5020 private String credDefId ;
5121
5222 private String credPreview = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview" ;
5323
54- private String cachedToken ;
55-
5624 public DICEAuth () {
5725 }
5826
59- public DICEAuth (String diceUrl , String diceApiUrl , String diceVerifier , String tenant , String username ,
60- String password , String scope , String clientId , String clientSecret , String credDefId ) {
27+ public DICEAuth (String diceUrl , String diceApiUrl , String diceVerifier , String diceApiKey , String credDefId ) {
6128 this .diceUrl = diceUrl ;
6229 this .diceApiUrl = diceApiUrl ;
6330 this .diceVerifier = diceVerifier ;
64- this .tenant = tenant ;
65- this .username = username ;
66- this .password = password ;
67- this .scope = scope ;
68- this .clientId = clientId ;
69- this .clientSecret = clientSecret ;
31+ this .diceApiKey = diceApiKey ;
7032 this .credDefId = credDefId ;
7133 }
7234
@@ -94,52 +56,12 @@ public void setDiceVerifier(String diceVerifier) {
9456 this .diceVerifier = diceVerifier ;
9557 }
9658
97- public String getTenant () {
98- return tenant ;
99- }
100-
101- public void setTenant (String tenant ) {
102- this .tenant = tenant ;
103- }
104-
105- public String getUsername () {
106- return username ;
107- }
108-
109- public void setUsername (String username ) {
110- this .username = username ;
111- }
112-
113- public String getPassword () {
114- return password ;
115- }
116-
117- public void setPassword (String password ) {
118- this .password = password ;
59+ public String getDiceApiKey () {
60+ return diceApiKey ;
11961 }
12062
121- public String getScope () {
122- return scope ;
123- }
124-
125- public void setScope (String scope ) {
126- this .scope = scope ;
127- }
128-
129- public String getClientId () {
130- return clientId ;
131- }
132-
133- public void setClientId (String clientId ) {
134- this .clientId = clientId ;
135- }
136-
137- public String getClientSecret () {
138- return clientSecret ;
139- }
140-
141- public void setClientSecret (String clientSecret ) {
142- this .clientSecret = clientSecret ;
63+ public void setDiceApiKey (String diceApiKey ) {
64+ this .diceApiKey = diceApiKey ;
14365 }
14466
14567 public String getCredDefId () {
@@ -157,56 +79,4 @@ public String getCredPreview() {
15779 public void setCredPreview (String credPreview ) {
15880 this .credPreview = credPreview ;
15981 }
160-
161- public String getToken () throws Exception {
162- Boolean isCachedTokenExpired = false ;
163- if (cachedToken != null ) {
164- if (getTokenExpiryTime (cachedToken ) <= 0 ) {
165- isCachedTokenExpired = true ;
166- logger .info ("Application cached token expired" );
167- }
168- }
169- if (cachedToken == null || isCachedTokenExpired ) {
170- String url = "https://login.microsoftonline.com/" + getTenant () + "/oauth2/v2.0/token" ;
171- Response response = new Request (url , "POST" )
172- .param ("grant_type" , "password" )
173- .param ("username" , getUsername ())
174- .param ("password" , getPassword ())
175- .param ("scope" , getScope ())
176- .param ("client_id" , getClientId ())
177- .param ("client_secret" , getClientSecret ()).execute ();
178- if (response .getStatusCode () != HttpURLConnection .HTTP_OK ) {
179- throw new APIRuntimeException (HttpURLConnection .HTTP_INTERNAL_ERROR ,
180- String .format ("Got unexpected response from remote service. %d %s" , response .getStatusCode (),
181- response .getText ()));
182- }
183- cachedToken = new ObjectMapper ().readValue (response .getText (), Auth0Credential .class ).getIdToken ();
184- logger .info ("Fetched token from URL: " + url );
185- }
186- return cachedToken ;
187- }
188-
189- /**
190- * Get token expiry time in seconds
191- *
192- * @param token JWT token
193- * throws Exception if any error occurs
194- * @return the Integer result
195- */
196- private Integer getTokenExpiryTime (String token ) throws Exception {
197- DecodedJWT decodedJWT = null ;
198- Integer tokenExpiryTime = 0 ;
199- if (token != null ) {
200- try {
201- decodedJWT = JWT .decode (token );
202- } catch (JWTDecodeException e ) {
203- throw new InvalidTokenException (token , "Error occurred in decoding token. " + e .getLocalizedMessage (),
204- e );
205- }
206- Date tokenExpiryDate = decodedJWT .getExpiresAt ();
207- Long tokenExpiryTimeInMilliSeconds = tokenExpiryDate .getTime () - (new Date ().getTime ()) - 60 * 1000 ;
208- tokenExpiryTime = (int ) Math .floor (tokenExpiryTimeInMilliSeconds / 1000 );
209- }
210- return tokenExpiryTime ;
211- }
21282}
0 commit comments