Auth0Client
class. A Concern called Secured
is used to authorize endpoints which require authentication through an incoming access token.If you have not created an API in your Auth0 dashboard yet, use the interactive selector to create a new Auth0 API or select an existing API for your project.To set up your first API through the Auth0 dashboard, review our getting started guide.Each Auth0 API uses the API Identifier, which your application needs to validate the access token.messages
resource if users have the manager access level, and a write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section.read:messages
scope.Auth0Client
. This class decodes and verifies the incoming access token taken from the Authorization
header of the request.The Auth0Client
class retrieves the public key for your Auth0 tenant and then uses it to verify the signature of the access token. The Token
struct defines a validate_permissions
method to look for a particular scope
in an access token by providing an array of required scopes and check if they are present in the payload of the token.Create a Concern called Secured
which looks for the access token in the Authorization
header of an incoming request.If the token is present, the Auth0Client.validate_token
will use the jwt
Gem to verify the token’s signature and validate the token’s claims.In addition to verifying that the access token is valid, the Concern also includes a mechanism for confirming the token has the sufficient scope to access the requested resources. In this example we define a validate_permissions
method that receives a block and checks the permissions by calling the Token.validate_permissions
method from the Auth0Client
class.For the /private-scoped
route, the scopes defined will be intersected with the scopes coming in the payload, to determine if it contains one or more items from the other array.By adding the Secure
concern to your application controller, you’ll only need to use a before_action
filter in the controller that requires authorization.Create a controller to handle the public endpoint /api/public
.The /public
endpoint does not require any authorization so no before_action
is needed.Create a controller to handle the private endpoints: /api/private
and /api/private-scoped
./api/private
is available for authenticated requests containing an access token with no additional scopes./api/private-scoped
is available for authenticated requests containing an access token with the read:messages
scope grantedThe protected endpoints need to call the authorize
method from the Secured
concern, for that you use before_action :authorize
, this ensure the Secured.authorize
method is called before every action in the PrivateController
.Authorization
header in your requests.GET /api/public
is available for non-authenticated requests.GET /api/private
is available for authenticated requests.GET /api/private-scoped
is available for authenticated requests containing an Access Token with the read:messages
scope.messages
resource if users have the manager access level, and a write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section.read:messages
scope.Auth0Client
. This class decodes and verifies the incoming access token taken from the Authorization
header of the request.The Auth0Client
class retrieves the public key for your Auth0 tenant and then uses it to verify the signature of the access token. The Token
struct defines a validate_permissions
method to look for a particular scope
in an access token by providing an array of required scopes and check if they are present in the payload of the token.Secured
which looks for the access token in the Authorization
header of an incoming request.If the token is present, the Auth0Client.validate_token
will use the jwt
Gem to verify the token’s signature and validate the token’s claims.In addition to verifying that the access token is valid, the Concern also includes a mechanism for confirming the token has the sufficient scope to access the requested resources. In this example we define a validate_permissions
method that receives a block and checks the permissions by calling the Token.validate_permissions
method from the Auth0Client
class.For the /private-scoped
route, the scopes defined will be intersected with the scopes coming in the payload, to determine if it contains one or more items from the other array.Secure
concern to your application controller, you’ll only need to use a before_action
filter in the controller that requires authorization./api/public
.The /public
endpoint does not require any authorization so no before_action
is needed./api/private
and /api/private-scoped
./api/private
is available for authenticated requests containing an access token with no additional scopes./api/private-scoped
is available for authenticated requests containing an access token with the read:messages
scope grantedThe protected endpoints need to call the authorize
method from the Secured
concern, for that you use before_action :authorize
, this ensure the Secured.authorize
method is called before every action in the PrivateController
.Authorization
header in your requests.GET /api/public
is available for non-authenticated requests.GET /api/private
is available for authenticated requests.GET /api/private-scoped
is available for authenticated requests containing an Access Token with the read:messages
scope.