other changes

This commit is contained in:
Joseph.Roy 2023-12-14 17:25:02 +00:00
parent 29c8bb368f
commit 62730b3f31
5 changed files with 57 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
bin/ bin/
auth/data/
*.db *.db

View File

@ -18,4 +18,3 @@ end
def generate_random_string(length) def generate_random_string(length)
SecureRandom.urlsafe_base64(length) SecureRandom.urlsafe_base64(length)
end end

View File

@ -76,7 +76,6 @@ def update_user_password(user_id, password)
end end
def get_claims (user_id) def get_claims (user_id)
puts "Getting claims for #{user_id}"
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME ) db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
results = db.execute(' results = db.execute('
SELECT C.claim SELECT C.claim

1
auth/example.txt Normal file

File diff suppressed because one or more lines are too long

53
auth/tokens.rb Normal file
View File

@ -0,0 +1,53 @@
def get_reauth_jwt (user_id)
#claims = get_claims user_id
iat = Time.now.to_i
exp = iat + 10
payload = {
sub: 'reauthentication' ,
admin: check_if_user_is_admin(user_id),
iss: 'roysathome.net',
uid: user_id, #Example id
iat: Time.now.to_i,
exp: Time.now.to_i + 3600
}
return JWT.encode payload, TOKEN_SECRET, 'HS256'
#data: {time: 'now', help: 'no'}.to_json
end
def get_jwt (user_id)
claims = get_claims user_id
iat = Time.now.to_i
exp = iat + 60
payload = {
sub: 'authentication' ,
admin: check_if_user_is_admin(user_id),
iss: 'roysathome.net',
uid: user_id, #Example id
iat: iat,
exp: exp,
claims: claims
}
return JWT.encode payload, TOKEN_SECRET, 'HS256'
end
def decode_token(base64_encoded_token)
return JWT.decode(base64_encoded_token, TOKEN_SECRET, true, algorithm: 'HS256')
end
def get_and_check_token(request)
authorization_header = request.env['HTTP_AUTHORIZATION']
unless authorization_header && authorization_header.match(/^Bearer (.+)/)
return nil
end
return reauth_token = Regexp.last_match(1)
end