Skip to content

Instantly share code, notes, and snippets.

@mattrw89
Created June 16, 2011 19:42
Show Gist options
  • Save mattrw89/1030067 to your computer and use it in GitHub Desktop.
Save mattrw89/1030067 to your computer and use it in GitHub Desktop.
def oauth_required(options = {})
if scope = options.delete(:scope)
before_filter options do |controller|
if controller.oauth.authenticated?
if !controller.oauth.scope.strip.split.include?(scope)
controller.send :head, controller.oauth.no_scope!(scope)
end
else
controller.send :head, controller.oauth.no_access!
end
end
else
before_filter :oauth_required, options
end
end
end
# Rejects the request and returns 401 (Unauthorized). You can just
# return 401, but this also sets the WWW-Authenticate header the right
# value.
#
# @return 401
def no_access!
@response["oauth.no_access"] = "true"
@response.status = 401
end
# Rejects the request and returns 403 (Forbidden). You can just
# return 403, but this also sets the WWW-Authenticate header the right
# value. Indicates which scope the client needs to make this request.
#
# @param [String] scope The missing scope, e.g. "read"
# @return 403
def no_scope!(scope)
@response["oauth.no_scope"] = scope.to_s
#render :json => "[hello!]" doesn't work
@response.status = 403
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment