Skip to content

Commit 187cfa3

Browse files
committed
implement basic role-based access control with auth0
1 parent 2c1a7de commit 187cfa3

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Rails/Ruby: API Basic Access Control Code Sample
1+
# Rails/Ruby: API Basic Role-Based Access Control (RBAC) Code Sample
22

3-
This Ruby code sample demonstrates **how to implement authorization** in Rails API servers using Auth0.
3+
This Ruby code sample demonstrates **how to implement Role-Based Access Control (RBAC)** in Rails API servers using Auth0.
44

55
This code sample is part of the ["Auth0 Developer Resources"](https://developer.auth0.com/resources), a place where you can explore the authentication and authorization features of the Auth0 Identity Platform.
66

7-
Visit the ["Rails/Ruby Code Sample: Authorization For Basic APIs"](https://developer.auth0.com/resources/code-samples/api/rails/basic-authorization) page for instructions on how to configure and run this code sample and how to integrate it with a Single-Page Application (SPA) of your choice.
7+
Visit the ["Rails/Ruby Code Sample: Role-Based Access Control For Basic APIs"](https://developer.auth0.com/resources/code-samples/api/rails/basic-role-based-access-control) page for instructions on how to configure and run this code sample and how to integrate it with a Single-Page Application (SPA) of your choice.
88

99
## Why Use Auth0?
1010

app/controllers/api/messages_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class MessagesController < BaseController
55
before_action :authorize, except: [:public]
66

77
def admin
8-
render json: Message.admin_message
8+
validate_permissions ['read:admin-messages'] do
9+
render json: Message.admin_message
10+
end
911
end
1012

1113
def protected

app/controllers/concerns/secured.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ module Secured
1212
error_description: 'Authorization header value must follow this format: Bearer access-token',
1313
message: 'Bad credentials'
1414
}.freeze
15+
INSUFFICIENT_PERMISSIONS = {
16+
error: 'insufficient_permissions',
17+
error_description: 'The access token does not contain the required permissions',
18+
message: 'Permission denied'
19+
}.freeze
1520

1621
def authorize
1722
token = token_from_request
@@ -20,9 +25,18 @@ def authorize
2025

2126
validation_response = Auth0Client.validate_token(token)
2227

28+
@decoded_token = validation_response.decoded_token
29+
2330
return unless (error = validation_response.error)
2431

25-
render json: {message: error.message}, status: error.status
32+
render json: { message: error.message }, status: error.status
33+
end
34+
35+
def validate_permissions(permissions)
36+
raise 'validate_permissions needs to be called with a block' unless block_given?
37+
return yield if @decoded_token.validate_permissions(permissions)
38+
39+
render json: INSUFFICIENT_PERMISSIONS, status: :forbidden
2640
end
2741

2842
private

app/lib/auth0_client.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ class Auth0Client
88
# Auth0 Client Objects
99
Error = Struct.new(:message, :status)
1010
Response = Struct.new(:decoded_token, :error)
11+
Token = Struct.new(:token) do
12+
def validate_permissions(permissions)
13+
required_permissions = Set.new permissions
14+
token_permissions = Set.new token[0]['permissions']
15+
required_permissions <= token_permissions
16+
end
17+
end
1118

1219
# Helper Functions
1320
def self.domain_url
@@ -43,7 +50,7 @@ def self.validate_token(token)
4350

4451
decoded_token = decode_token(token, jwks_hash)
4552

46-
Response.new(decoded_token, nil)
53+
Response.new(Token.new(decoded_token), nil)
4754
rescue JWT::VerificationError, JWT::DecodeError => e
4855
error = Error.new('Bad credentials', :unauthorized)
4956
Response.new(nil, error)

0 commit comments

Comments
 (0)