aws-amplify / amplify-android

The fastest and easiest way to use AWS from your Android app.
https://docs.amplify.aws/lib/q/platform/android/
Apache License 2.0
249 stars 117 forks source link

Amplify.Auth.fetchAuthSession return isSigned = false. #2946

Closed Mehdi-android closed 1 week ago

Mehdi-android commented 1 week ago

Before opening, please confirm:

Language and Async Model

Kotlin

Amplify Categories

Authentication

Gradle script dependencies

```groovy // Put output below this line implementation("com.amazonaws:aws-android-sdk-core:2.30.0") implementation("com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.30.0") implementation("com.amplifyframework:core:2.24.0") implementation("com.amplifyframework:aws-auth-cognito:2.24.0") implementation("com.amplifyframework:core-kotlin:2.24.0") ```

Environment information

``` # Put output below this line ```

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

I am using Amplify android for authentication in my application. I signed in using Amplify.Auth.signIn but when I close application and start again and call Amplify.Auth.fetchAuthSession I get below error AWSCognitoAuthSession(isSignedIn=false, identityIdResult=AuthSessionResult{value=ap-southeast-1:xxxxx-xxxx-xxxxxxxxxxxx, error=null, type=SUCCESS},. Also I use AWSSdk for awssigner authorization `class AwsInterceptor( private val awsCredentials: CognitoCachingCredentialsProvider, // AWS credentials private val serviceName: String, // Service to sign requests for (e.g., execute-api) region: String // AWS region (e.g., ap-southeast-1) ) : Interceptor {

private val signer: AWS4Signer = AWS4Signer()

init {
    signer.setServiceName(serviceName)
    signer.setRegionName(region)
}

override fun intercept(chain: Interceptor.Chain): Response {
    val signedRequest = signRequest(chain.request())
    return chain.proceed(signedRequest)
}

@Throws(IOException::class)
private fun signRequest(request: Request): Request {
    val builder = request.newBuilder()
    val awsRequest = DefaultRequest<Any>(serviceName)
    builder.addHeader("x-api-key", "I513UYRoQT4SNGrHDzWFS6wfKw1e9b9J9UaT24vk")
    // Set endpoint and add query params
    val url = setEndpoint(builder, awsRequest, request.url)
    setQueryParams(awsRequest, url)

    // Set HTTP method
    setHttpMethod(awsRequest, request.method)

    // Set body (if any)
    setBody(awsRequest, request.body)

    // Sign the request with AWS4Signer and credentials
    signer.sign(awsRequest, awsCredentials.credentials)

    // Apply signed headers to the request
    applyAwsHeaders(builder, awsRequest.headers)

    return builder.build()
}

private fun setEndpoint(
    builder: Request.Builder,
    awsRequest: DefaultRequest<Any>,
    url: HttpUrl
): HttpUrl {
    val canonicalUrl = ensureTrailingSlash(builder, url)
    awsRequest.endpoint = canonicalUrl.toUri()
    return canonicalUrl
}

private fun setQueryParams(awsRequest: DefaultRequest<Any>, url: HttpUrl) {
    for (paramName in url.queryParameterNames) {
        awsRequest.addParameter(paramName, url.queryParameter(paramName))
    }
}

private fun setHttpMethod(awsRequest: DefaultRequest<Any>, method: String) {
    val methodName = HttpMethodName.valueOf(method)
    awsRequest.httpMethod = methodName
}

@Throws(IOException::class)
private fun setBody(awsRequest: DefaultRequest<Any>, body: RequestBody?) {
    if (body == null) return

    val buffer = Buffer()
    body.writeTo(buffer)
    awsRequest.content = ByteArrayInputStream(buffer.readByteArray())
    awsRequest.addHeader("Content-Length", body.contentLength().toString())
    buffer.close()
}

private fun applyAwsHeaders(builder: Request.Builder, headers: Map<String, String>) {
    for ((key, value) in headers) {
        builder.header(key, value)
    }
}

private fun ensureTrailingSlash(builder: Request.Builder, url: HttpUrl): HttpUrl {
    val lastPathSegment = url.pathSegments.lastOrNull()
    if (!lastPathSegment.isNullOrEmpty()) {
        val updatedUrl = url.newBuilder().addPathSegment("").build()
        builder.url(updatedUrl)
        return updatedUrl
    }
    return url
}

}`

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

amplifyconfiguration.json

_ { "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "auth": { "plugins": { "awsCognitoAuthPlugin": { "UserAgent": "aws-amplify-cli/0.1.0", "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "ap-southeast-1:xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx", "Region": "ap-southeast-1" } } }, "CognitoUserPool": { "Default": { "PoolId": "ap-southeast-1_xxxxxxx", "AppClientId": "xxxxxxxxxxxx", "AppClientSecret": "xxxxxxxxxxxxxxxxxxx", "Region": "ap-southeast-1" } }, "Auth": { "Default": { "authenticationFlowType": "CUSTOMAUTH", "socialProviders": [], "usernameAttributes": [ "EMAIL" ], "signupAttributes": [ "EMAIL" ], "passwordProtectionSettings": { "passwordPolicyMinLength": 8, "passwordPolicyCharacters": [] }, "mfaConfiguration": "OPTIONAL", "mfaTypes": [ "EMAIL" ], "verificationMechanisms": [ "EMAIL" ] } } } } } }

GraphQL Schema

```graphql // Put your schema below this line ```

Additional information and screenshots

No response

edisooon commented 1 week ago

Hi @Mehdi-android, thank you for submitting the issue! One of our team members would investigate it as soon as possible. In the meantime, could you share your logs as well as session details before and after relaunching the app? This will help our investigation.

Mehdi-android commented 1 week ago

Actually issue was I was using amazonawscore as well as amplify sdk, which was causing the issue. I am only using amplify now which resolved the issue.

github-actions[bot] commented 1 week ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.