-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53cd6de
commit 7621056
Showing
7 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
require "../auth_handler" | ||
require "jwt" | ||
require "../../config" | ||
|
||
module LavinMQ | ||
class OAuth2Handler < LavinMQ::AuthHandler | ||
def initialize(@users : UserStore) | ||
end | ||
|
||
# Temporary for tests | ||
@token : String = LavinMQ::Config.instance.token | ||
@public_key : String = LavinMQ::Config.instance.public_key | ||
|
||
def authenticate(username : String, password : String) | ||
# TODO: implement the OAuth2 authentication logic and permissions parser here | ||
if password.starts_with?("oauth") | ||
puts "OAuth2 authentication successful" | ||
@users[username] | ||
else | ||
puts "OAuth2 authentication failed" | ||
begin | ||
payload, header = JWT.decode(@token, key: @public_key, algorithm: JWT::Algorithm::RS256, verify: true, validate: true) | ||
oauth_user | ||
rescue ex : JWT::DecodeError | ||
@log.warn { "OAuth2 authentication failed, could not decode token: #{ex}" } | ||
try_next(username, password) | ||
rescue ex : JWT::UnsupportedAlgorithmError | ||
@log.warn { "OAuth2 authentication failed, unsupported algortihm: #{ex}" } | ||
try_next(username, password) | ||
rescue ex | ||
@log.warn { "OAuth2 authentication failed: #{ex}" } | ||
try_next(username, password) | ||
end | ||
end | ||
|
||
def oauth_user | ||
# TODO: Create a uset that will be deleted when it disconnects, but also cannot be authorised with basic auth. | ||
# introduce the needed configs for validation, and parse the payload to get the user details | ||
user = @users.create("oauth_user", "password") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters