Skip to content

Commit 041cf22

Browse files
create jwt encode and decode util funcs
1 parent f6f2281 commit 041cf22

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/jwt_util.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module JWTUtil
2+
include RSAUtil
3+
4+
def self::encode(user)
5+
JWT.encode(
6+
{
7+
iss: 'probably digitalocean or some shit',
8+
iat: Time.now.to_i,
9+
sub: user.id + '.' + Time.now.to_s
10+
},
11+
OpenSSL::PKey::RSA.new(RSAUtil::Keys::priv, ENV['PASSPHRASE']),
12+
'RS256'
13+
)
14+
end
15+
16+
def self::decode(token)
17+
begin
18+
JWT.decode(
19+
token,
20+
OpenSSL::PKey::RSA.new(RSAUtil::Keys::pub, ENV['PASSPHRASE']),
21+
true,
22+
{ algorithm: 'RS256' }
23+
)
24+
rescue JWT::DecodeError
25+
return nil
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)