Git Product home page Git Product logo

Comments (2)

henritoivar avatar henritoivar commented on May 31, 2024

You should pass in your own client, which implements the exchange of tokens:

$this->userClient = new JWTClient($team->tenant, null, $this->createUserClient($team->tenant));

The client, which does the token exchange and then uses the user specific token would roughly be something like this:

    private function createUserClient(Tenant $tenant)
    {
        $user = auth()->user();
        if (!$user || !$user->jira_id) {
            return null;
        }

        // TODO: Reuse user token if exists and not expired
        // We can reuse the same token for 15 minutes

        // Generate exchange token
        $exchangeToken = JWT::encode([
            'iss' => 'urn:atlassian:connect:clientid:' . $tenant->oauth_client_token,
            'sub' => 'urn:atlassian:connect:useraccountid:' . $user->jira_id,
            'tnt' => $tenant->base_url,
            'aud' => 'https://oauth-2-authorization-server.services.atlassian.com',
            'iat' => time(),
            'exp' => time() + 30

        ], $tenant->shared_secret);

        // Exchange token for user token
        $authServerClient = new Client([
            'base_uri' => 'https://oauth-2-authorization-server.services.atlassian.com',
            'headers' => [
                'Accept' => 'application/json',
            ]
        ]);

        $res = $authServerClient->request('POST', 'oauth2/token', [
            'form_params' => [
                'assertion' => $exchangeToken,
                'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'
            ]
        ]);
        $access = json_decode($res->getBody(), true);

        //TODO: Store token

        // Use user token to act on behalf of user

        $stack = new \GuzzleHttp\HandlerStack();
        $stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());

        $stack->push(\GuzzleHttp\Middleware::mapRequest(
            function (\Psr\Http\Message\RequestInterface $request)
            use ($access) {
                return new \GuzzleHttp\Psr7\Request(
                    $request->getMethod(),
                    $request->getUri(),
                    array_merge($request->getHeaders(), ['Authorization' => $access['token_type'] . ' ' . $access['access_token']]),
                    $request->getBody()
                );
            }));

        return new Client(['handler' => $stack]);
    }

from atlassian-connect-core.

jessiechia avatar jessiechia commented on May 31, 2024

That looks great. Thanks @henritoivar!

from atlassian-connect-core.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.