|
| 1 | +package au.org.ala.userdetails |
| 2 | + |
| 3 | +import com.amazonaws.AmazonWebServiceResult |
| 4 | +import com.amazonaws.ResponseMetadata |
| 5 | +import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider |
| 6 | +import com.amazonaws.services.cognitoidp.model.CreateUserPoolClientRequest |
| 7 | +import com.amazonaws.services.cognitoidp.model.CreateUserPoolClientResult |
| 8 | +import com.amazonaws.services.cognitoidp.model.DeleteUserPoolClientRequest |
| 9 | +import com.amazonaws.services.cognitoidp.model.DescribeUserPoolClientRequest |
| 10 | +import com.amazonaws.services.cognitoidp.model.UpdateUserPoolClientRequest |
| 11 | +import com.amazonaws.services.cognitoidp.model.UserPoolClientType |
| 12 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB |
| 13 | +import com.amazonaws.services.dynamodbv2.model.AttributeValue |
| 14 | +import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest |
| 15 | +import com.amazonaws.services.dynamodbv2.model.PutItemRequest |
| 16 | +import com.amazonaws.services.dynamodbv2.model.QueryRequest |
| 17 | + |
| 18 | +class CognitoApplicationService implements IApplicationService { |
| 19 | + |
| 20 | + IUserService userService |
| 21 | + AWSCognitoIdentityProvider cognitoIdp |
| 22 | + String poolId |
| 23 | + |
| 24 | +// Config config |
| 25 | + List<String> supportedIdentityProviders |
| 26 | + List<String> authFlows |
| 27 | + List<String> clientScopes |
| 28 | + List<String> galahCallbackURLs |
| 29 | + List<String> tokensCallbackURLs |
| 30 | + |
| 31 | + AmazonDynamoDB dynamoDB |
| 32 | + String dynamoDBTable |
| 33 | + String dynamoDBPK |
| 34 | + String dynamoDBSK |
| 35 | + |
| 36 | + List<ApplicationRecord> listApplicationsForUser(String userId) { |
| 37 | + def qr = new QueryRequest() |
| 38 | + .withTableName(dynamoDBTable) |
| 39 | + .withKeyConditionExpression("$dynamoDBPK = :userId") |
| 40 | + .withExpressionAttributeValues([":userId": new AttributeValue(userId)]) |
| 41 | + def result = dynamoDB.query(qr) |
| 42 | + |
| 43 | + if (result.sdkHttpMetadata.httpStatusCode == 200) { |
| 44 | + result.items.collect { itemToApplication(it) } |
| 45 | + } else { |
| 46 | + throw new RuntimeException("Could not list clients for user $userId") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private ApplicationRecord itemToApplication(item) { |
| 51 | + def clientId = item.get(dynamoDBSK).getS() |
| 52 | + |
| 53 | + def client = cognitoIdp.describeUserPoolClient( |
| 54 | + new DescribeUserPoolClientRequest() |
| 55 | + .withUserPoolId(poolId) |
| 56 | + .withClientId(clientId) |
| 57 | + ) |
| 58 | + userPoolClientToApplication(client.userPoolClient) |
| 59 | + } |
| 60 | + |
| 61 | + private ApplicationRecord userPoolClientToApplication(UserPoolClientType userPoolClient) { |
| 62 | + def name = userPoolClient.clientName |
| 63 | + def clientId = userPoolClient.clientId |
| 64 | + def secret = userPoolClient.clientSecret |
| 65 | + def callbackUrls = userPoolClient.callbackURLs |
| 66 | + def allowedFlows = userPoolClient.allowedOAuthFlows |
| 67 | + userPoolClient.logoutURLs |
| 68 | + userPoolClient.defaultRedirectURI |
| 69 | + |
| 70 | + def type |
| 71 | + if (allowedFlows.contains('client_credentials')) { |
| 72 | + type = ApplicationType.M2M |
| 73 | + } else if (allowedFlows.contains('code')) { |
| 74 | + if (userPoolClient.clientSecret) { |
| 75 | + type = ApplicationType.CONFIDENTIAL |
| 76 | + } else { |
| 77 | + type = ApplicationType.PUBLIC |
| 78 | + } |
| 79 | + } else { |
| 80 | + type = ApplicationType.UNKNOWN |
| 81 | + } |
| 82 | + |
| 83 | + return new ApplicationRecord( |
| 84 | + name: name, |
| 85 | + clientId: clientId, |
| 86 | + secret: secret, |
| 87 | + callbacks: callbackUrls, |
| 88 | + type: type, |
| 89 | + needTokenAppAsCallback: callbackUrls?.containsAll(tokensCallbackURLs) |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + List<String> listClientIdsForUser(String userId) { |
| 94 | + listApplicationsForUser(userId).collect { it.clientId } |
| 95 | + } |
| 96 | + |
| 97 | + private def addClientIdForUser(String userId, String clientId) { |
| 98 | + def putResponse = dynamoDB.putItem( |
| 99 | + new PutItemRequest(dynamoDBTable, [(dynamoDBPK): new AttributeValue(userId), (dynamoDBSK): new AttributeValue(clientId)])) |
| 100 | + if (putResponse.sdkHttpMetadata.httpStatusCode != 200) { |
| 101 | + throw new RuntimeException("Couldn't add mapping for $clientId to $userId") |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private def deleteClientIdForUser(String userId, String clientId) { |
| 106 | + def deleteResponse = dynamoDB.deleteItem( |
| 107 | + new DeleteItemRequest(dynamoDBTable, [(dynamoDBPK): new AttributeValue(userId), (dynamoDBSK): new AttributeValue(clientId)])) |
| 108 | + if (deleteResponse.sdkHttpMetadata.httpStatusCode != 200) { |
| 109 | + throw new RuntimeException("Couldn't delete mapping for $clientId to $userId") |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private def getClientByUserIdAndClientId(String userId, String clientId) { |
| 114 | + def result = dynamoDB.getItem(dynamoDBTable, [(dynamoDBPK): new AttributeValue(userId), (dynamoDBSK): new AttributeValue(clientId)]) |
| 115 | + return result.item |
| 116 | + } |
| 117 | + |
| 118 | + private def isUserOwnsClientId(String userId, String clientId) { |
| 119 | + return getClientByUserIdAndClientId(userId, clientId) != null |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + ApplicationRecord generateClient(String userId, ApplicationRecord applicationRecord) { |
| 124 | + CreateUserPoolClientRequest request = new CreateUserPoolClientRequest().withUserPoolId(poolId) |
| 125 | + request.clientName = applicationRecord.name |
| 126 | + // TODO enable user consent |
| 127 | + if (applicationRecord.type == ApplicationType.M2M) { |
| 128 | + request.generateSecret = true |
| 129 | + request.allowedOAuthFlows = ["client_credentials"] |
| 130 | + } else { |
| 131 | + request.generateSecret = applicationRecord.type == ApplicationType.CONFIDENTIAL //do not need secret for public clients |
| 132 | + request.allowedOAuthFlows = ["code"] |
| 133 | + } |
| 134 | + request.supportedIdentityProviders = new ArrayList<>(supportedIdentityProviders) |
| 135 | + request.preventUserExistenceErrors = "ENABLED" |
| 136 | + request.explicitAuthFlows = new ArrayList<>(authFlows) |
| 137 | + request.allowedOAuthFlowsUserPoolClient = true |
| 138 | + |
| 139 | + def scopes = new ArrayList<>(clientScopes) |
| 140 | + |
| 141 | + if (scopes && applicationRecord.type != ApplicationType.M2M) { |
| 142 | + request.allowedOAuthScopes = scopes |
| 143 | + } |
| 144 | + if(applicationRecord.type == ApplicationType.M2M) { |
| 145 | + request.allowedOAuthScopes = ["ala/attrs"] |
| 146 | + } |
| 147 | + |
| 148 | + request.callbackURLs = new ArrayList<>(applicationRecord.callbacks.findAll{it != ""}) |
| 149 | + if (applicationRecord.type == ApplicationType.M2M) { |
| 150 | + request.callbackURLs = null |
| 151 | + } |
| 152 | + else if(applicationRecord.needTokenAppAsCallback) { |
| 153 | + request.callbackURLs.addAll(tokensCallbackURLs) |
| 154 | + } |
| 155 | + |
| 156 | + CreateUserPoolClientResult response = cognitoIdp.createUserPoolClient(request) |
| 157 | + |
| 158 | + if (isSuccessful(response)) { |
| 159 | + def clientId = response.userPoolClient.clientId |
| 160 | + addClientIdForUser(userId, clientId) |
| 161 | + return userPoolClientToApplication(response.userPoolClient) |
| 162 | + } else { |
| 163 | + throw new RuntimeException("Could not generate client") |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + void updateClient(String userId, ApplicationRecord applicationRecord) { |
| 169 | + if (!isUserOwnsClientId(userId, applicationRecord.clientId)) { |
| 170 | + throw new IllegalArgumentException("${applicationRecord.clientId} not found") |
| 171 | + } |
| 172 | + def request = new UpdateUserPoolClientRequest().withUserPoolId(poolId) |
| 173 | + request.withClientId(applicationRecord.clientId) |
| 174 | + request.withClientName(applicationRecord.name) |
| 175 | + request.supportedIdentityProviders = new ArrayList<>(supportedIdentityProviders) |
| 176 | + request.preventUserExistenceErrors = "ENABLED" |
| 177 | + request.explicitAuthFlows = new ArrayList<>(authFlows) |
| 178 | + request.allowedOAuthFlowsUserPoolClient = true |
| 179 | + |
| 180 | + if (applicationRecord.type == ApplicationType.M2M) { |
| 181 | + request.allowedOAuthFlows = ["client_credentials"] |
| 182 | + } else { |
| 183 | + request.allowedOAuthFlows = ["code"] |
| 184 | + } |
| 185 | + |
| 186 | + def scopes = new ArrayList<>(clientScopes) |
| 187 | + |
| 188 | + if (scopes && applicationRecord.type != ApplicationType.M2M) { |
| 189 | + request.allowedOAuthScopes = scopes |
| 190 | + } |
| 191 | + if(applicationRecord.type == ApplicationType.M2M) { |
| 192 | + request.allowedOAuthScopes = ["ala/attrs"] |
| 193 | + } |
| 194 | + |
| 195 | + request.callbackURLs = new ArrayList<>(applicationRecord.callbacks.findAll{it != ""}) |
| 196 | + if (applicationRecord.type == ApplicationType.M2M) { |
| 197 | + request.callbackURLs = null |
| 198 | + } |
| 199 | + else if(applicationRecord.needTokenAppAsCallback) { |
| 200 | + request.callbackURLs.addAll(tokensCallbackURLs) |
| 201 | + } |
| 202 | + |
| 203 | + def response = cognitoIdp.updateUserPoolClient(request) |
| 204 | + if (!isSuccessful(response)) { |
| 205 | + throw new RuntimeException("Could not update client $applicationRecord.clientId") |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + ApplicationRecord findClientByClientId(String userId, String clientId) { |
| 211 | + return itemToApplication(getClientByUserIdAndClientId(userId, clientId)) |
| 212 | + } |
| 213 | + |
| 214 | + private static boolean isSuccessful(AmazonWebServiceResult<? extends ResponseMetadata> result) { |
| 215 | + def code = result.sdkHttpMetadata.httpStatusCode |
| 216 | + return code >= 200 && code < 300 |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + boolean deleteApplication(String userId, String clientId){ |
| 221 | + if (!isUserOwnsClientId(userId, clientId)) { |
| 222 | + throw new IllegalArgumentException("${clientId} not found") |
| 223 | + } |
| 224 | + def request = new DeleteUserPoolClientRequest().withUserPoolId(poolId).withClientId(clientId) |
| 225 | + |
| 226 | + def response = cognitoIdp.deleteUserPoolClient(request) |
| 227 | + if (!isSuccessful(response)) { |
| 228 | + throw new RuntimeException("Could not delete client $clientId") |
| 229 | + } |
| 230 | + else{ |
| 231 | + deleteClientIdForUser(userId, clientId) |
| 232 | + return true |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments