1212*/
1313package io .kubernetes .client .util .authenticators ;
1414
15+ import com .microsoft .aad .adal4j .AuthenticationContext ;
16+ import com .microsoft .aad .adal4j .AuthenticationResult ;
1517import io .kubernetes .client .util .KubeConfig ;
18+ import java .net .MalformedURLException ;
1619import java .util .Date ;
1720import java .util .Map ;
21+ import java .util .concurrent .ExecutionException ;
22+ import java .util .concurrent .Executors ;
23+ import java .util .concurrent .Future ;
1824
1925/**
2026 * The Authenticator interface represents a plugin that can handle a specific type of authentication
@@ -27,6 +33,9 @@ public class AzureActiveDirectoryAuthenticator implements Authenticator {
2733
2834 private static final String ACCESS_TOKEN = "access-token" ;
2935 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" ;
3039
3140 @ Override
3241 public String getName () {
@@ -50,6 +59,26 @@ public boolean isExpired(Map<String, Object> config) {
5059
5160 @ Override
5261 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+ }
5483 }
5584}
0 commit comments