stream-chat-android icon indicating copy to clipboard operation
stream-chat-android copied to clipboard

JWTAuth error on setting user.

Open Chikrix opened this issue 7 years ago • 7 comments

Whenever I set a user, I get an error code 5 with the message JWTAuth error: json: cannot unmarshal object into Go struct field StreamJWTClaims.exp of type int64. I have no idea what could be the cause or how I can resolve it. I believe I'm using a valid token because I'm making the request to get the token and use the tokenSuccessListener.onSuccess to get my token while setting the user. Any idea how I can resolve this?

Chikrix avatar Oct 21 '19 16:10 Chikrix

@tbarbugli

tschellenbach avatar Oct 21 '19 16:10 tschellenbach

@Chikrix can you share the token here? you can redact a token by stripping out the last section of the token.

somevery.weirdbase64.token -> somevery.weirdbase64.

tbarbugli avatar Oct 21 '19 17:10 tbarbugli

also, it would be great if you could share the code you used to generate the token. Looking at the error I'd say something is off with the exp field

tbarbugli avatar Oct 21 '19 17:10 tbarbugli

@tbarbugli Here's my auth token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMjg0LTExNjk5MTcxIiwiZXhwIjp7ImRhdGUiOiIyMDE5LTEwLTIyIDEzOjM2OjI4LjkxNzI0NCIsInRpbWV6b25lX3R5cGUiOjMsInRpbWV6b25lIjoiQW1lcmljYVwvRGVudmVyIn19.

here's my code snippet

/**
     * This uses Get Stream Client Library to hit their services
     * @param array $user
     * @return string
     * @throws StreamChatServiceException
     * @throws \GetStream\StreamChat\StreamException
     */
    public function addUser(array $user): string
    {
        if (empty($user)) {
            throw new StreamChatServiceException("A valid user is required");
        }

        $this->getStreamChat->updateUser($user);
        // with an expiration time
        $expiration = (Carbon::now()->addMinutes(1440));
        $userToken = $this->getStreamChat->createToken($user['id'], $expiration);

        return $userToken;
    }

The above calls the following method from https://github.com/GetStream/stream-chat-php

/**
     * @param  string $userId
     * @param  array $extraData
     * @return string
     */
    public function createToken($userId, $expiration=null)
    {
        $payload = [
            'user_id'   => $userId,
        ];
        if($expiration !== null){
            $payload['exp'] = $expiration;
        }
        return JWT::encode($payload, $this->apiSecret, 'HS256');
    }

Chikrix avatar Oct 21 '19 19:10 Chikrix

@Chikrix exp must be a timestamp (ie. something like this Carbon::now()->addMinutes(1440)->timestamp) ; @pterk can you add docs and a test to the PHP client?

tbarbugli avatar Oct 22 '19 10:10 tbarbugli

Thanks @tbarbugli . Would try that.

Chikrix avatar Oct 22 '19 15:10 Chikrix

FTR, a note, a check and a test were added to the PHP client so it should be easier to debug this in future.

pterk avatar Oct 29 '19 20:10 pterk