|
@@ -18,7 +18,6 @@ import java.util.Date;
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
-
|
|
|
@Slf4j
|
|
|
@Getter
|
|
|
@Setter
|
|
@@ -28,20 +27,34 @@ public class AuthDelegateImpl implements IAuthDelegate {
|
|
|
private String lastErrMsg;
|
|
|
private AipConfig aipConfig;
|
|
|
private String refreshToken;
|
|
|
- private ConfidentialClientApplication confidentialApp;
|
|
|
+ private ConfidentialClientApplication confidentialApp = null;
|
|
|
private IAuthenticationResult authenticationResult = null;
|
|
|
+
|
|
|
+ private Date expiredDate = new Date();
|
|
|
+ private String accessToken = "x";
|
|
|
+
|
|
|
private final String instance = "https://login.microsoftonline.com/";
|
|
|
private final String authority = "https://login.windows.net/";
|
|
|
private final String scope = "https://graph.microsoft.com/.default";
|
|
|
|
|
|
+ private static int LOGIN_TYPE = 0;
|
|
|
+ private static String CLIENT_ID = "";
|
|
|
+ private static String TENANT_ID = "";
|
|
|
+ private static String AUTHORITY = "";
|
|
|
+ private static Set<String> SCOPE = Collections.singleton("");
|
|
|
+
|
|
|
public AuthDelegateImpl(AipConfig aipConfig) throws Exception {
|
|
|
this.aipConfig = aipConfig;
|
|
|
resetError();
|
|
|
|
|
|
String cacheKey = aipConfig.getClientId() + "_" + aipConfig.getTenantId() + "_AppTokenCache";
|
|
|
MemoryTokenCacheWithEviction memoryTokenCacheWithEviction = new MemoryTokenCacheWithEviction(cacheKey);
|
|
|
+ //TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("token_cache.json");
|
|
|
|
|
|
+ CLIENT_ID = aipConfig.getClientId();
|
|
|
+ TENANT_ID = aipConfig.getTenantId();
|
|
|
if (aipConfig.getLoginType() == AipConfig.authLoginPassword) {
|
|
|
+ LOGIN_TYPE = 0;
|
|
|
confidentialApp = ConfidentialClientApplication.builder(
|
|
|
aipConfig.getClientId(),
|
|
|
ClientCredentialFactory.createFromSecret(aipConfig.getSecretValue()))
|
|
@@ -50,6 +63,7 @@ public class AuthDelegateImpl implements IAuthDelegate {
|
|
|
.build();
|
|
|
}
|
|
|
else if (aipConfig.getLoginType() == AipConfig.authLoginCert) {
|
|
|
+ LOGIN_TYPE = 1;
|
|
|
InputStream certStream = new ByteArrayInputStream(Files.readAllBytes(Paths.get(aipConfig.getCertThumbPrint())));
|
|
|
|
|
|
String password = "hanteinfo1234!";
|
|
@@ -63,73 +77,193 @@ public class AuthDelegateImpl implements IAuthDelegate {
|
|
|
else {
|
|
|
throw new Exception("지원하지 않는 AIP 인증 방법 입니다.");
|
|
|
}
|
|
|
- }
|
|
|
- public void resetError() {
|
|
|
- lastErrNo = 0;
|
|
|
- lastErrMsg = "";
|
|
|
- }
|
|
|
- public void setError(int errNo, String errMsg1, String errMsg2) {
|
|
|
- lastErrNo = errNo;
|
|
|
- if (errMsg2 == null || errMsg2.isEmpty()) {
|
|
|
- lastErrMsg = errMsg1;
|
|
|
+ if (confidentialApp != null) {
|
|
|
+ log.info("AuthDelegateImpl: Refresh token provider setting.");
|
|
|
+ //confidentialApp.appTokenProvider();
|
|
|
}
|
|
|
- else {
|
|
|
- lastErrMsg = errMsg1 + "\r\n" + errMsg2;
|
|
|
- }
|
|
|
- log.error("AuthDelegateImpl:setError, {}, {}, {}", lastErrNo, errMsg1, errMsg2);
|
|
|
}
|
|
|
@Override
|
|
|
public String acquireToken(Identity identity, String authority, String resource, String claim) {
|
|
|
- //log.error("acquireToken--------------------------------");
|
|
|
if (authenticationResult != null) {
|
|
|
if (authenticationResult.expiresOnDate().before(new Date())) {
|
|
|
log.error("Access token expired #################################################################################################");
|
|
|
}
|
|
|
}
|
|
|
return AcquireTokenByCertificate(identity, authority, resource, claim);
|
|
|
- //return authenticationResult.accessToken();
|
|
|
}
|
|
|
+ private static IAuthenticationResult acquireTokenInteractive() throws Exception {
|
|
|
+
|
|
|
+ // Load token cache from file and initialize token cache aspect. The token cache will have
|
|
|
+ // dummy data, so the acquireTokenSilently call will fail.
|
|
|
+ //TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");
|
|
|
+ String cacheKey = CLIENT_ID + "_" + TENANT_ID + "_AppTokenCache";
|
|
|
+ MemoryTokenCacheWithEviction memoryTokenCacheWithEviction = new MemoryTokenCacheWithEviction(cacheKey);
|
|
|
+
|
|
|
+ ConfidentialClientApplication pca = ConfidentialClientApplication.builder(
|
|
|
+ CLIENT_ID,
|
|
|
+ ClientCredentialFactory.createFromSecret("CvW8Q~0iANtLN1Y2EXR_nVyYb_tQTDwjW-Z7Ndg3"))
|
|
|
+ .authority("https://login.microsoftonline.com/" + TENANT_ID)
|
|
|
+ .setTokenCacheAccessAspect(memoryTokenCacheWithEviction)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Set<IAccount> accountsInCache = pca.getAccounts().join();
|
|
|
+ // Take first account in the cache. In a production application, you would filter
|
|
|
+ // accountsInCache to get the right account for the user authenticating.
|
|
|
+ IAccount account = accountsInCache.iterator().next();
|
|
|
+
|
|
|
+ IAuthenticationResult result;
|
|
|
+ try {
|
|
|
+ SilentParameters silentParameters =
|
|
|
+ SilentParameters
|
|
|
+ .builder(SCOPE, account)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // try to acquire token silently. This call will fail since the token cache
|
|
|
+ // does not have any data for the user you are trying to acquire a token for
|
|
|
+ result = pca.acquireTokenSilently(silentParameters).join();
|
|
|
+ } catch (Exception ex) {
|
|
|
+ if (ex.getCause() instanceof MsalException) {
|
|
|
+
|
|
|
+ ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(SCOPE).build();
|
|
|
+ result = pca.acquireToken(clientCredentialParam).get();
|
|
|
+ } else {
|
|
|
+ // Handle other exceptions accordingly
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
private String AcquireTokenByCertificate(Identity identity, String authority, String resource, String claims)
|
|
|
{
|
|
|
- // AuthenticationResult result;
|
|
|
- // var authorityUri = new Uri(authority);
|
|
|
- // authority = $"https://{authorityUri.Host}/{_aipConfig.TenantId}";
|
|
|
- Set<String> scope;
|
|
|
+ Set<String> scope = null;
|
|
|
if (resource.endsWith("/")){
|
|
|
scope = Collections.singleton(resource + ".default");
|
|
|
}
|
|
|
else {
|
|
|
scope = Collections.singleton(resource + "/.default");
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
- ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
|
|
|
- scope
|
|
|
- )
|
|
|
- .build();
|
|
|
-
|
|
|
-// RefreshTokenParameters refreshTokenParameters = RefreshTokenParameters
|
|
|
-// .builder(
|
|
|
-// scope,
|
|
|
-// refreshToken)
|
|
|
-// .build();
|
|
|
+ // authority scope
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // Wed Jun 05 10:41:59 KST 2024
|
|
|
+ // https://login.windows.net/2e58414a-c6ae-43ff-aaf5-45ab8b78a404 https://aadrm.com/.default
|
|
|
|
|
|
- //RefreshTokenParameters refreshTokenParam = RefreshTokenParameters.builder().build();
|
|
|
- //confidentialApp.acquireTokenSilently(clientCredentialParam);
|
|
|
- //log.error("AcquireTokenByCertificate--------------------------------Before");
|
|
|
+ log.info("acquireToken: identity = {}, authority = {}, scope = {}", identity.getEmail(), authority, scope);
|
|
|
+ try {
|
|
|
+ ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(scope).build();
|
|
|
CompletableFuture<IAuthenticationResult> future = confidentialApp.acquireToken(clientCredentialParam);
|
|
|
authenticationResult = future.get();
|
|
|
- //log.error("AcquireTokenByCertificate---------------------------------After");
|
|
|
+ if (!expiredDate.equals(authenticationResult.expiresOnDate())) {
|
|
|
+ log.info("acquireToken: expiredDate = {}, expiresOnDate = {}", expiredDate, authenticationResult.expiresOnDate());
|
|
|
+ expiredDate = authenticationResult.expiresOnDate();
|
|
|
+ }
|
|
|
+ if (!accessToken.equals(authenticationResult.accessToken())) {
|
|
|
+ log.info("acquireToken: accessToken: OLD = {}", accessToken);
|
|
|
+ log.info("acquireToken: accessToken: NEW = {}", authenticationResult.accessToken());
|
|
|
+ accessToken = authenticationResult.accessToken();
|
|
|
+ }
|
|
|
return authenticationResult.accessToken();
|
|
|
}
|
|
|
catch (Exception ex) {
|
|
|
if (ex.getCause() instanceof MsalException) {
|
|
|
- setError(1, "AcquireTokenByCertificate::AcquireTokenByCertificate, Scope provided is not supported.", ex.getMessage());
|
|
|
+ setError(1, "AuthDelegateImpl:acquireToken, Scope provided is not supported.", ex.getMessage());
|
|
|
} else {
|
|
|
- setError(1, "AcquireTokenByCertificate::AcquireTokenByCertificate Failed.", ex.getMessage());
|
|
|
+ setError(1, "AuthDelegateImpl:acquireToken Failed.", ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return null;
|
|
|
}
|
|
|
+ private String AcquireTokenByCertificate2(Identity identity, String authority, String resource, String claims)
|
|
|
+ {
|
|
|
+ Set<String> scope = null;
|
|
|
+ if (resource.endsWith("/")){
|
|
|
+ scope = Collections.singleton(resource + ".default");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ scope = Collections.singleton(resource + "/.default");
|
|
|
+ }
|
|
|
+ // authority scope
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://syncservice.o365syncservice.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // https://login.windows.net/common https://aadrm.com/.default
|
|
|
+ // Wed Jun 05 10:41:59 KST 2024
|
|
|
+ // https://login.windows.net/2e58414a-c6ae-43ff-aaf5-45ab8b78a404 https://aadrm.com/.default
|
|
|
+
|
|
|
+ log.info("acquireToken: identity = {}, authority = {}, scope = {}", identity.getEmail(), authority, scope);
|
|
|
+ try {
|
|
|
+ Set<IAccount> accountsInCache = confidentialApp.getAccounts().get();
|
|
|
+ // Take first account in the cache. In a production application, you would filter accountsInCache to get the right account for the user authenticating.
|
|
|
+ IAccount account = accountsInCache.iterator().next();
|
|
|
+ SilentParameters silentParameters = SilentParameters .builder(scope, account) .build();
|
|
|
+ CompletableFuture<IAuthenticationResult> future = confidentialApp.acquireTokenSilently(silentParameters);
|
|
|
+ authenticationResult = future.get();
|
|
|
+ }
|
|
|
+ catch (Exception ex) {
|
|
|
+ try {
|
|
|
+ if (ex.getCause() instanceof MsalException) {
|
|
|
+ log.error("AuthDelegateImpl:acquireToken refresh");
|
|
|
+ ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(scope).build();
|
|
|
+ CompletableFuture<IAuthenticationResult> future = confidentialApp.acquireToken(clientCredentialParam);
|
|
|
+ authenticationResult = future.get();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ setError(1, "AuthDelegateImpl:acquireToken acquireTokenSilently Exception.", ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ setError(2, "AuthDelegateImpl:acquireToken MsalException acquireToken Exception Failed.", ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!accessToken.equals(authenticationResult.accessToken())) {
|
|
|
+ log.info("acquireToken: accessToken: OLD = {}", accessToken);
|
|
|
+ log.info("acquireToken: accessToken: NEW = {}", authenticationResult.accessToken());
|
|
|
+ accessToken = authenticationResult.accessToken();
|
|
|
+ }
|
|
|
+ return authenticationResult.accessToken();
|
|
|
+
|
|
|
+// try {
|
|
|
+// ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(scope).build();
|
|
|
+// CompletableFuture<IAuthenticationResult> future = confidentialApp.acquireToken(clientCredentialParam);
|
|
|
+// authenticationResult = future.get();
|
|
|
+// if (!expiredDate.equals(authenticationResult.expiresOnDate())) {
|
|
|
+// log.info("acquireToken: expiredDate = {}, expiresOnDate = {}", expiredDate, authenticationResult.expiresOnDate());
|
|
|
+// expiredDate = authenticationResult.expiresOnDate();
|
|
|
+// }
|
|
|
+// if (!accessToken.equals(authenticationResult.accessToken())) {
|
|
|
+// log.info("acquireToken: accessToken: OLD = {}", accessToken);
|
|
|
+// log.info("acquireToken: accessToken: NEW = {}", authenticationResult.accessToken());
|
|
|
+// accessToken = authenticationResult.accessToken();
|
|
|
+// }
|
|
|
+// return authenticationResult.accessToken();
|
|
|
+// }
|
|
|
+// catch (Exception ex) {
|
|
|
+// if (ex.getCause() instanceof MsalException) {
|
|
|
+// setError(1, "AuthDelegateImpl:acquireToken, Scope provided is not supported.", ex.getMessage());
|
|
|
+// } else {
|
|
|
+// setError(1, "AuthDelegateImpl:acquireToken Failed.", ex.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ public void resetError() {
|
|
|
+ lastErrNo = 0;
|
|
|
+ lastErrMsg = "";
|
|
|
+ }
|
|
|
+ public void setError(int errNo, String errMsg1, String errMsg2) {
|
|
|
+ lastErrNo = errNo;
|
|
|
+ if (errMsg2 == null || errMsg2.isEmpty()) {
|
|
|
+ lastErrMsg = errMsg1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ lastErrMsg = errMsg1 + "\r\n" + errMsg2;
|
|
|
+ }
|
|
|
+ log.error("AuthDelegateImpl:setError, {}, {}, {}", lastErrNo, errMsg1, errMsg2);
|
|
|
+ }
|
|
|
}
|