12
12
*/
13
13
package io .kubernetes .client .util .authenticators ;
14
14
15
+ import com .microsoft .aad .adal4j .AuthenticationContext ;
16
+ import com .microsoft .aad .adal4j .AuthenticationResult ;
15
17
import io .kubernetes .client .util .KubeConfig ;
18
+ import java .net .MalformedURLException ;
16
19
import java .util .Date ;
17
20
import java .util .Map ;
21
+ import java .util .concurrent .ExecutionException ;
22
+ import java .util .concurrent .Executors ;
23
+ import java .util .concurrent .Future ;
18
24
19
25
/**
20
26
* The Authenticator interface represents a plugin that can handle a specific type of authentication
@@ -27,6 +33,9 @@ public class AzureActiveDirectoryAuthenticator implements Authenticator {
27
33
28
34
private static final String ACCESS_TOKEN = "access-token" ;
29
35
private static final String EXPIRES_ON = "expires-on" ;
36
+ private static final String TENANT_ID = "tenant-id" ;
37
+ private static final String CLIENT_ID = "client-id" ;
38
+ private static final String REFRESH_TOKEN = "refresh-token" ;
30
39
31
40
@ Override
32
41
public String getName () {
@@ -50,6 +59,26 @@ public boolean isExpired(Map<String, Object> config) {
50
59
51
60
@ Override
52
61
public Map <String , Object > refresh (Map <String , Object > config ) {
53
- throw new RuntimeException ("Unimplemented" );
62
+ // TODO: Support national clouds!
63
+ String cloud = "https://login.microsoftonline.com" ;
64
+ String tenantId = (String ) config .get (TENANT_ID );
65
+ String authority = cloud + "/" + tenantId ;
66
+ String clientId = (String ) config .get (CLIENT_ID );
67
+ String refreshToken = (String ) config .get (REFRESH_TOKEN );
68
+
69
+ try {
70
+ AuthenticationContext context =
71
+ new AuthenticationContext (authority , true , Executors .newSingleThreadExecutor ());
72
+ Future <AuthenticationResult > resultFuture =
73
+ context .acquireTokenByRefreshToken (refreshToken , clientId , null );
74
+ AuthenticationResult result = resultFuture .get ();
75
+ config .put (ACCESS_TOKEN , result .getAccessToken ());
76
+ config .put (REFRESH_TOKEN , result .getRefreshToken ());
77
+
78
+ return config ;
79
+
80
+ } catch (InterruptedException | MalformedURLException | ExecutionException ex ) {
81
+ throw new RuntimeException (ex );
82
+ }
54
83
}
55
84
}
0 commit comments