Skip to content

Commit 74b5dd3

Browse files
committed
documented anonymous access
1 parent e48ceba commit 74b5dd3

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

Resources/doc/index.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,60 @@ security:
465465
pattern: ^/api
466466
fos_oauth: true
467467
stateless: true
468+
anonymous: false # can be omitted as its default value
468469

469470
access_control:
470-
# You can omit this if /api can be accessed both authenticated and anonymously
471471
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
472472
```
473473
474474
The URLs under `/api` will use OAuth2 to authenticate users.
475475

476+
#### Anonymous access
477+
478+
Sometimes you need to allow your api to be accessed without authorization. In order to do that lets adjust
479+
above-mentioned example configuration.
480+
481+
``` yaml
482+
# app/config/security.yml
483+
security:
484+
firewalls:
485+
oauth_token:
486+
pattern: ^/oauth/v2/token
487+
security: false
488+
489+
oauth_authorize:
490+
pattern: ^/oauth/v2/auth
491+
# Add your favorite authentication process here
492+
493+
api:
494+
pattern: ^/api
495+
fos_oauth: true
496+
stateless: true
497+
anonymous: true # note that anonymous access is now enabled
498+
499+
# also note absence of "access_control" section
500+
```
501+
502+
From now on all of your api resources can be accessed without authorization. But what if one or more of them should be
503+
secured anyway or/and require presence of authenticated user? It's easy! You can do that manually by adding few lines of
504+
code at the beginning of all of your secured actions like in the example below:
505+
506+
``` php
507+
// [...]
508+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
509+
510+
class YourApiController extends Controller
511+
{
512+
public function getSecureResourceAction()
513+
{
514+
# this is it
515+
if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
516+
throw new AccessDeniedException();
517+
}
518+
519+
// [...]
520+
}
521+
```
476522

477523
### Step 5: Configure FOSOAuthServerBundle
478524

0 commit comments

Comments
 (0)