|
| 1 | +/* |
| 2 | + * Copyright 2013-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.cognito; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import org.springframework.util.Assert; |
| 21 | +import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient; |
| 22 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AdminCreateUserRequest; |
| 23 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AdminCreateUserResponse; |
| 24 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AdminInitiateAuthRequest; |
| 25 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AdminInitiateAuthResponse; |
| 26 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AttributeType; |
| 27 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.AuthFlowType; |
| 28 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.ChallengeNameType; |
| 29 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.ConfirmForgotPasswordRequest; |
| 30 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.ConfirmForgotPasswordResponse; |
| 31 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.ForgotPasswordRequest; |
| 32 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.ForgotPasswordResponse; |
| 33 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.RespondToAuthChallengeRequest; |
| 34 | +import software.amazon.awssdk.services.cognitoidentityprovider.model.RespondToAuthChallengeResponse; |
| 35 | + |
| 36 | +/** |
| 37 | + * Higher level abstraction over {@link CognitoIdentityProviderClient} providing methods for the most common auth |
| 38 | + * operations |
| 39 | + * |
| 40 | + * @author Oleh Onufryk |
| 41 | + * @since 3.3.0 |
| 42 | + */ |
| 43 | + |
| 44 | +public class CognitoTemplate implements CognitoAuthOperations { |
| 45 | + |
| 46 | + private final CognitoIdentityProviderClient cognitoIdentityProviderClient; |
| 47 | + private final String clientId; |
| 48 | + private final String userPoolId; |
| 49 | + private final String clientSecret; |
| 50 | + |
| 51 | + public CognitoTemplate(CognitoIdentityProviderClient cognitoIdentityProviderClient, String clientId, |
| 52 | + String userPoolId, String clientSecret) { |
| 53 | + Assert.notNull(cognitoIdentityProviderClient, "cognitoIdentityProviderClient is required"); |
| 54 | + Assert.notNull(clientId, "clientId is required"); |
| 55 | + Assert.notNull(userPoolId, "userPoolId is required"); |
| 56 | + this.cognitoIdentityProviderClient = cognitoIdentityProviderClient; |
| 57 | + this.clientId = clientId; |
| 58 | + this.userPoolId = userPoolId; |
| 59 | + this.clientSecret = clientSecret; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public AdminInitiateAuthResponse login(String username, String password) { |
| 64 | + AdminInitiateAuthRequest adminInitiateAuthRequest = AdminInitiateAuthRequest.builder().userPoolId(userPoolId) |
| 65 | + .clientId(clientId).authFlow(AuthFlowType.ADMIN_USER_PASSWORD_AUTH) |
| 66 | + .authParameters(resolveAuthParameters(username, password)).build(); |
| 67 | + return cognitoIdentityProviderClient.adminInitiateAuth(adminInitiateAuthRequest); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public AdminCreateUserResponse createUser(String username, List<AttributeType> attributeTypes) { |
| 72 | + AdminCreateUserRequest createUserRequest = AdminCreateUserRequest.builder().userPoolId(userPoolId) |
| 73 | + .username(username).userAttributes(attributeTypes).build(); |
| 74 | + return cognitoIdentityProviderClient.adminCreateUser(createUserRequest); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ForgotPasswordResponse resetPassword(String username) { |
| 79 | + ForgotPasswordRequest forgotPasswordRequest = ForgotPasswordRequest.builder().clientId(clientId) |
| 80 | + .username(username).build(); |
| 81 | + |
| 82 | + return cognitoIdentityProviderClient.forgotPassword(forgotPasswordRequest); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ConfirmForgotPasswordResponse confirmResetPassword(String username, String confirmationCode, |
| 87 | + String newPassword) { |
| 88 | + ConfirmForgotPasswordRequest confirmForgotPasswordRequest = ConfirmForgotPasswordRequest.builder() |
| 89 | + .clientId(clientId).username(username).password(newPassword).confirmationCode(confirmationCode) |
| 90 | + .secretHash(CognitoUtils.calculateSecretHash(clientId, clientSecret, username)).build(); |
| 91 | + return cognitoIdentityProviderClient.confirmForgotPassword(confirmForgotPasswordRequest); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public RespondToAuthChallengeResponse setPermanentPassword(String session, String username, String password) { |
| 96 | + RespondToAuthChallengeRequest respondToAuthChallengeRequest = RespondToAuthChallengeRequest.builder() |
| 97 | + .clientId(clientId).challengeName(ChallengeNameType.NEW_PASSWORD_REQUIRED) |
| 98 | + .challengeResponses(Map.of(CognitoParameters.USERNAME_PARAM_NAME, username, |
| 99 | + CognitoParameters.NEW_PASSWORD_PARAM_NAME, password, CognitoParameters.SECRET_HASH_PARAM_NAME, |
| 100 | + CognitoUtils.calculateSecretHash(clientId, clientSecret, username))) |
| 101 | + .build(); |
| 102 | + return cognitoIdentityProviderClient.respondToAuthChallenge(respondToAuthChallengeRequest); |
| 103 | + } |
| 104 | + |
| 105 | + private Map<String, String> resolveAuthParameters(String username, String password) { |
| 106 | + return Map.of(CognitoParameters.USERNAME_PARAM_NAME, username, CognitoParameters.PASSWORD_PARAM_NAME, password, |
| 107 | + CognitoParameters.SECRET_HASH_PARAM_NAME, |
| 108 | + CognitoUtils.calculateSecretHash(clientId, clientSecret, username)); |
| 109 | + } |
| 110 | +} |
0 commit comments