/** * Find an access token. */ //??? internal static AccessToken FindAccessToken(string accessTokenId) { // Debug.Tested(); // Debug.AssertString(accessTokenId); // AccessToken retVal = null; // if (AccessTokens.ContainsKey(accessTokenId)) { // retVal = AccessTokens[accessTokenId]; // Debug.AssertValid(retVal); // } // return retVal; // } /** * Find a user's access token. * Returns null if an access token is not found for the user. */ internal static async Task <AccessToken> FindAccessTokenByUserID(AmazonDynamoDBClient dbClient, string userId) { Debug.Untested(); Debug.AssertValid(dbClient); Debug.AssertID(userId); AccessToken retVal = null; Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue>(); key.Add(IdentityServiceDataLayer.FIELD_ACCESS_TOKENS_USER_ID, new AttributeValue(userId)); GetItemResponse getResponse = await dbClient.GetItemAsync(IdentityServiceDataLayer.DATASET_ACCESS_TOKENS_INDEX_USER_ID, key); Debug.AssertValid(getResponse); Debug.AssertValidOrNull(getResponse.Item); if (getResponse.Item != null) { // An access token with the specified ID exists. Debug.Untested(); retVal = IdentityServiceDataLayer.AccessTokenFromDBItem(getResponse.Item); Debug.AssertValid(retVal); } return(retVal); /*??--AccessToken retVal = null; * foreach (var item in AccessTokens) { * Debug.AssertValid(item); * Debug.AssertString(item.Key); * Debug.AssertValidOrNull(item.Value); * if (item.Value != null) { * Debug.Assert(item.Value.ID == item.Key); * Debug.AssertValidOrNull(item.Value.User); * if (item.Value.User != null) { * if (item.Value.User.ID == userId) { * retVal = item.Value; * break; * } * } * } * } * return retVal;*/ }
/** * Get the ID of the logged in user from the access token. * If the access token has expired then treat it as not existing and return zero (i.e. invalid ID). */ internal static async Task <string> UserIDFromAccessToken(AmazonDynamoDBClient dbClient, string accessTokenID) { Debug.Untested(); Debug.AssertValidOrNull(accessTokenID); string retVal = Helper.INVALID_ID; if (!string.IsNullOrEmpty(accessTokenID)) { Debug.Untested(); var accessTokenDBItem = await IdentityServiceDataLayer.GetAccessTokenDBItemById(dbClient, accessTokenID); Debug.AssertValidOrNull(accessTokenDBItem); if (accessTokenDBItem != null) { // An access token with the specified ID exists. Debug.Untested(); AccessToken accessToken = IdentityServiceDataLayer.AccessTokenFromDBItem(accessTokenDBItem); Debug.AssertValid(accessToken); if (accessToken.Expires > DateTime.Now) { // The access token has not expired. Debug.Untested(); Debug.AssertID(accessToken.UserID); retVal = accessToken.UserID; } else { // The access token has expired. Debug.Untested(); } } else { // Access token not found. Debug.Tested(); } } else { // Invalid (empty or null) access token. Debug.Untested(); } //??-- if (AccessTokens.ContainsKey(accessTokenID)) { // Debug.Tested(); // AccessToken accessToken = AccessTokens[accessTokenID]; // Debug.AssertValid(accessToken); // Debug.AssertValidOrNull(accessToken.User); // if (accessToken.Expires > DateTime.Now) { // // The access token has not expired. // Debug.Tested(); // if (accessToken.User != null) { // // Access token associated with a user. // Debug.Tested(); // Debug.AssertID(accessToken.User.ID); // retVal = accessToken.User.ID; // } else { // // Access token not associated with a user. // Debug.Untested(); // } // } // else // { // // The access token has expired. // Debug.Untested(); // } // } else { // // Access token not found. // Debug.Tested(); // } // } else { // // Invalid (empty or null) access token. // Debug.Untested(); // } return(retVal); }