Calling (Admin)InitiateAuthRequest in the AWS Cognito To Get an Access Token with Scopes

Viewed 2048

I need to expose an api, which also allows us to get the scope, but I'm failing with all my attempts using aws cognito.

So I was hoping to do the following:

  1. assign scope:foo to existing users and new users
  2. get an access token back containing that scope of foo (using c# back end code)

Part I: Getting Access Token with Scope

So for getting a token it's the following:

var provider = new AmazonCognitoIdentityProviderClient(new BasicAWSCredentials(AccessKey, SecretKey));
var request1 = new AdminInitiateAuthRequest
            {
                UserPoolId = userPoolId,
                ClientId = request.ClientId,
                AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH,
                AuthParameters =
                {
                    {"USERNAME", request.UserName},
                    {"PASSWORD", request.Password}
                }
            };
var response1 = await provider.InitiateAuthAsync(request);

Now, I can gain the tokens with this request/response (obviously with good credentials :) ). What I cannot figure out is how to ask for more scope items. Currently it only returns aws.cognito.signin.user.admin

Part II: User Attributes

With that said, I've also added a custom:some-attribute attribute. I can see it within UI for the General settings \ Attributes screen. I don't have any lambda triggers setup, BTW.

I'm finding that I can update the user settings, as I get an OK|200 status returned, but if I try and get the user, his updated attribute doesn't show up.

var request2 = new AdminUpdateUserAttributesRequest
            {
                UserAttributes = new List<AttributeType>
                    {
                       new AttributeType {
                           Name="custom:some-attribute",
                           Value=Guid.NewGuid().ToString("N")
                    };
                UserPoolId = userPoolId,
                Username = userName
            };

var response2 = await provider.AdminUpdateUserAttributesAsync(request);

Later I go to look up the user:

var result = await _provider.GetUserAsync(new GetUserRequest
        {
            AccessToken = response1.AuthenticationResult.AccessToken
        });

... my newly updated variable doesn't show in the result.UserAttribues AttributeType list.

Maybe I'm doing this wrong, but I cannot figure out how to create a scope (which I was thinking I could do an alternative via the custom attribute) and get the scope.

Equally, I cannot figure out how to pass in a scope when authenticating for tokens.

Thanks, Kelly

1 Answers

Wow, I FINALLY figured this out...

  1. I still haven't quite figured this one out, and it would be great to, but with the custom attributes coming in (number 2). I may check this off as OK.

  2. getting custom attributes within the identity token. You have to set he read write of the attribute. there is more information on this here: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html. Basically Select the area, and it expands Attributes to reveal information you can turn on or off with an area for read and another for write. so uh, click click and Save app client changes! Note, these changes will enable them to show up within the Identity Token.

Related