# OAuth2 with Expressive 2 - Config help needed

**URL:** <https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470>\
**Category:** Mezzio\
**Tags:** expressive\
**Created:** [February 10, 2018, 8:29pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470 "2018-02-10T20:29:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 10, 2018, 8:29pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/1 "2018-02-10T20:29:10Z")

</div>

Hi,

I progress on my new api rest project and is time to include some authentication… I know that it exist [https://docs.zendframework.com/zend-expressive-authentication-oauth2/](https://docs.zendframework.com/zend-expressive-authentication-oauth2/) , it is exactly what i need but it still in dev and the documentation is empty at this time… I need to authenticate my requests via OAuth2 and work also with roles.

Someone already use it, implement it or have a little doc?

Thanks!

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [February 10, 2018, 9:58pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/2 "2018-02-10T21:58:26Z")

</div>

> **[zendframework/zend-expressive-authentication-oauth2](https://github.com/zendframework/zend-expressive-authentication-oauth2/)**
>
> OAuth2 (server) authentication middleware for PSR-7 applications. - zendframework/zend-expressive-authentication-oauth2

  
seems to be in `alpha1`.

The API should be fairly stable from now on (it will be stable once it  
reaches beta1), but as far as I understand, the focus is to ship Expressive  
3.0.0 (with PSR-15 support) first.

From what I can see, all you’d need is:

- adapting  
[https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/config/oauth2.php#L12-L24](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/config/oauth2.php#L12-L24)  
to your needs (make a new file)
- include the ConfigProvider -  
[https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/src/ConfigProvider.php](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/src/ConfigProvider.php)
- map the OAuth2Middleware in your routes -  
[https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/src/OAuth2Middleware.php#L19](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/31bf2a78979fab0a6d2d375c1086cb8f86dbdf61/src/OAuth2Middleware.php#L19)  
(this one looks like a request handler, rather than a middleware - worth  
reporting an issue)

The only bit I’m missing is how to set it up as a firewall in front of  
other middleware, which I couldn’t figure out by just looking these 5m at  
the code.

Marco Pivetta

[http://twitter.com/Ocramius](http://twitter.com/Ocramius)

[http://ocramius.github.com/](http://ocramius.github.com/)

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 11, 2018, 8:46pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/3 "2018-02-11T20:46:45Z")

</div>

Thanks for your answer! Yes i already tried different things in the past weeks.

I added the ConfigProvider files in config.php

```
... 
\Zend\Expressive\Authentication\ConfigProvider::class,
\Zend\Expressive\Authentication\OAuth2\ConfigProvider::class,
...

```

- Completed the config/oauth2.php in zend-expressive-authentication-oauth2 folder
- Or added a authentication.local.php file ([https://docs.zendframework.com/zend-expressive-authentication/v1/user-repository/](https://docs.zendframework.com/zend-expressive-authentication/v1/user-repository/))
- Added the sql oauth2 tables in my database (zend-expressive-authentication-oauth2/data/oauth2.sql)
- Created an /oauth route

Just for testing i added OAuth2Middleware in one of my route like that:

```
$app->get('/customer/{id}', [Zend\Expressive\Authentication\OAuth2\OAuth2Middleware::class,
                             App\Action\CustomerAction::class], 'customer.get');

```

And i got a error:

```
Zend\ServiceManager\Exception\ServiceNotCreatedException thrown with message
"Service with name "League\OAuth2\Server\AuthorizationServer" could not be created.
Reason: OAuth2 User Repository is missing"

```

I know that i miss something but i’m little lost with all this dependency, factory, aliases.

In the doc of zend-expressive-authentication i read that i need to add 2 aliases in the config for the adapter, but with which value?

```
'aliases' => [
            AuthenticationInterface::class => Zend\Expressive\Authentication\OAuth2\OAuth2Adapter::class,
            UserRepositoryInterface::class => Zend\Expressive\Authentication\OAuth2\UserRepositoryInterface::class
],

```

This seems to have no effect and i’m really not sure if is the right values…

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [February 12, 2018, 11:56am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/4 "2018-02-12T11:56:27Z")

</div>

Could you try to get a full trace for that exception?

Also, this section likely needs aliases pointing to concrete instances:

‘aliases’ =\> [  
AuthenticationInterface::class =\>  
Zend\Expressive\Authentication\OAuth2\OAuth2Adapter::class,  
UserRepositoryInterface::class =\>  
Zend\Expressive\Authentication\OAuth2\UserRepositoryInterface::class  
],

Marco Pivetta

[http://twitter.com/Ocramius](http://twitter.com/Ocramius)

[http://ocramius.github.com/](http://ocramius.github.com/)

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [February 12, 2018, 5:16pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/5 "2018-02-12T17:16:32Z")

</div>

@MichaelB you right, the documentation is missing for `zend-expressive-authentication-oauth2`. I have this task in my TODO list and I’ll write asap.

In the meantime, regarding your error msg _“OAuth2 User Repository is missing”_ it seems there is no configuration for `UserRepositoryInterface::class`. The default value is `UserRepositoryInterface::class => Pdo\UserRepository` in the aliases of `ConfigProvider.php` file. This is a UserRepository implementation using a PDO database.

You can see a “working” example in test [OAuth2PdoMiddlewareTest](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/test/Pdo/OAuth2PdoMiddlewareTest.php). Let me know if you need more information.

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 12, 2018, 9:02pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/6 "2018-02-12T21:02:14Z")

</div>

Hi, @ocramius and @enrico thanks for your answers!

About my error “OAuth2 User Repository is missing”, i first removed 2 aliases that i added (by mistake) in my own ConfigProvider.php. In all case i still have the same error.

Now in the **ConfigProvider.php on Zend\Expressive\Authentication\OAuth2** i just discovered that it miss this 2 lines:

```
...
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
...

```

And that line in “aliases” array is probably wrong:

```
AuthCodeEntityInterface::class => Pdo\AuthCodeRepository::class,

```

I replaced by:

```
AuthCodeRepositoryInterface::class => Pdo\AuthCodeRepository::class,

```

**Is it possible?**

Now i got a json response and i think i’m in the good way… no ?

```
{"error":"unsupported_grant_type","message":"The authorization grant type is not supported by the authorization server.","hint":"Check the `grant_type` parameter"}
```

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [February 13, 2018, 5:59pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/7 "2018-02-13T17:59:36Z")

</div>

Hi @MichaelB, yes you are in the right way 🙂  
The error message suggests that you are not using a valid `grant_type`. You can check the `phpleage/oauth2-server` [documentation](https://oauth2.thephpleague.com/authorization-server/which-grant/) regarding the grant type to use and the parameters to pass.  
You can also check the usage in [OAuth2PdoMiddlewareTest.php](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/test/Pdo/OAuth2PdoMiddlewareTest.php).

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 14, 2018, 10:21pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/8 "2018-02-14T22:21:12Z")

</div>

Hi @enrico, i can now get an JWT “access\_token”! Thanks! 🙂

```
$app->route('/access_token', Zend\Expressive\Authentication\OAuth2\OAuth2Middleware::class, ['GET', 'POST'], 'oauth');

```

But i have now i little stupid question… I follow some doc and see also your “zendcon2017-api-tutorial”.  
For authentication in my action, “zend-expressive-authentication” is used and i can retrieve a UserInterface::class in my action like in your tutorial. So i added this line **in my module ConfigProvider.php and adapted for oauth2**

```
...
use Zend\Expressive\Authentication\AuthenticationInterface;
use Zend\Expressive\Authentication\UserRepositoryInterface;
...

'aliases' => [
         AuthenticationInterface::class => Zend\Expressive\Authentication\OAuth2\OAuth2Adapter::class,
         UserRepositoryInterface::class => Zend\Expressive\Authentication\OAuth2\UserRepository::class
],
...

```

**My route:**

```
$app->get('/customer/{id}', [Zend\Expressive\Authentication\AuthenticationMiddleware::class,
App\Action\CustomerAction::class], 'customer.get');

```

I guess is almost correct… or not… but i got **this error:**

```
Service with name "Zend\Expressive\Authentication\AuthenticationMiddleware" could not be created.
Reason: AuthenticationInterface service is missing

```

**Where this AuthenticationInterface service have to be create? Can you just help me please…**

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [February 15, 2018, 7:54am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/9 "2018-02-15T07:54:40Z")

</div>

Congrats on getting working the `access_token`! The `Zend\Expressive\Authentication\AuthenticationMiddleware` is managed by [zend-expressive-authentication](https://github.com/zendframework/zend-expressive-authentication). You need to include it in your composer.json. Basically, the idea is to use this generic authentication middleware with different adapters (in your case `zend-expressive-authentication-oauth2`). This authentication middleware provides the same interface for all the adapters and exposes a [UserInterface](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/UserInterface.php) class to be used as a common reference for the User object, [shared in the pipeline as PSR-7 attribute](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/AuthenticationMiddleware.php#L33).  
You can read the [documentation of zend-expressive-authentication](https://docs.zendframework.com/zend-expressive-authentication/) for more information.

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 15, 2018, 8:08am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/10 "2018-02-15T08:08:36Z")

</div>

Thanks @enrico but i already have zend-expressive-authentication installed and ConfigProvider is also loaded in the config.php file… is the reason i’m little blocked 😉

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [February 15, 2018, 8:15am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/11 "2018-02-15T08:15:12Z")

</div>

Can you share the code with us/me on github? In this way, I can help you more easily. Thanks!

---

<div class="post-metadata">

**Author:** ![MichaelB](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelB](https://discourse.laminas.dev/u/MichaelB)\
**Post date:** [February 15, 2018, 9:10am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/12 "2018-02-15T09:10:29Z")

</div>

I don’t have a github account, i’ll see tomorrow. Thanks!

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [March 1, 2018, 5:14pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/13 "2018-03-01T17:14:50Z")

</div>

I just sent this PR [https://github.com/zendframework/zend-expressive-authentication-oauth2/pull/19](https://github.com/zendframework/zend-expressive-authentication-oauth2/pull/19) containing the OAuth2 documentation. Comments and feedbacks are welcome, thanks!

---

<div class="post-metadata">

**Author:** ![sankhat\_vijay](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sankhat_vijay/32/363_2.png) [@sankhat\_vijay](https://discourse.laminas.dev/u/sankhat_vijay)\
**Post date:** [August 2, 2018, 7:24am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/14 "2018-08-02T07:24:33Z")

</div>

I had the same issue and fixed everything as per this thread, now I am getting an error like

> {“error”:“unknown\_error”,“message”:“It was not possible to parse your key, reason: error:0906D06C:PEM routines:PEM\_read\_bio:no start line”}

… any help would be appreciated.  
thanks in advance.

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [August 2, 2018, 8:59am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/15 "2018-08-02T08:59:29Z")

</div>

> [@sankhat\_vijay](#):
>
> {“error”:“unknown\_error”,“message”:“It was not possible to parse your key, reason: error:0906D06C:PEM routines:PEM\_read\_bio:no start line”}

This is probably caused by an invalid certificate, which can have several reasons:

- You are using the wrong file as a certificate (mixed up the cert and the key)
- On windows it could be caused by a certificate not compatible with windows (invalid characters).
- On windows invalid encoding. It should be UTF-8 without BOM.

For the next time, please open a new topic as this is a completely different issue. It might help others to find this if they encounter the same issue.

---

<div class="post-metadata">

**Author:** ![sankhat\_vijay](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sankhat_vijay/32/363_2.png) [@sankhat\_vijay](https://discourse.laminas.dev/u/sankhat_vijay)\
**Post date:** [August 2, 2018, 9:03am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/16 "2018-08-02T09:03:07Z")

</div>

@xtreamwayz Thanks for your replay. I’ll open new issue if the suggestion from your side not work.

---

<div class="post-metadata">

**Author:** ![sankhat\_vijay](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sankhat_vijay/32/363_2.png) [@sankhat\_vijay](https://discourse.laminas.dev/u/sankhat_vijay)\
**Post date:** [August 2, 2018, 9:30am UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/17 "2018-08-02T09:30:40Z")

</div>

@xtreamwayz Thanks a lot dear you saved my day. it worked for me… 😄

---

<div class="post-metadata">

**Author:** ![xha1](https://avatars.discourse-cdn.com/v4/letter/x/c4cdca/32.png) [@xha1](https://discourse.laminas.dev/u/xha1)\
**Post date:** [December 13, 2019, 1:59pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/18 "2019-12-13T13:59:24Z")

</div>

Hello together,

i got the same Error:  
{  
“error”: “unsupported\_grant\_type”,  
“message”: “The authorization grant type is not supported by the authorization server.”,  
“hint”: “Check the `grant_type` parameter”  
}

in my oauth2.global.php:  
‘grants’ =\> [  
Grant\ClientCredentialsGrant::class =\> Grant\ClientCredentialsGrant::class,  
Grant\PasswordGrant::class =\> Grant\PasswordGrant::class,  
Grant\AuthCodeGrant::class =\> Grant\AuthCodeGrant::class,  
Grant\ImplicitGrant::class =\> Grant\ImplicitGrant::class,  
Grant\RefreshTokenGrant::class =\> Grant\RefreshTokenGrant::class,  
],

---

<div class="post-metadata">

**Author:** ![lostpacket](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/lostpacket/32/570_2.png) [@lostpacket](https://discourse.laminas.dev/u/lostpacket)\
**Post date:** [December 13, 2019, 5:16pm UTC](https://discourse.laminas.dev/t/oauth2-with-expressive-2-config-help-needed/470/19 "2019-12-13T17:16:39Z")

</div>

When sending to the /oauth2/authorize endpoint? What data are you sending?
