roysathome.net/auth/tokens.rb

53 lines
1.2 KiB
Ruby

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