omniauth-auth0 3.1.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'json'
5
+ require 'jwt'
6
+
7
+ describe OmniAuth::Auth0::JWTToken do
8
+ let(:client_id) { 'CLIENT_ID' }
9
+ let(:domain_url) { 'https://samples.auth0.com' }
10
+ let(:client_assertion_signing_key) { OpenSSL::PKey::RSA.generate(2048) }
11
+
12
+ describe '#jwt_token' do
13
+ it 'generates a valid JWT token' do
14
+ uuid = '12345678-1234-5678-1234-567812345678'
15
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
16
+
17
+ jwt_token = described_class.new(client_id,
18
+ domain_url,
19
+ client_assertion_signing_key,
20
+ 'RS256')
21
+ .jwt_token
22
+ decoded_token = JWT.decode(jwt_token, client_assertion_signing_key, true, { algorithm: 'RS256' })
23
+
24
+ expect(decoded_token[0]['iss']).to eq(client_id)
25
+ expect(decoded_token[0]['sub']).to eq(client_id)
26
+ expect(decoded_token[0]['aud']).to eq("#{domain_url}/oauth/token")
27
+ expect(decoded_token[0]['iat']).to be_within(5).of(Time.now.utc.to_i)
28
+ expect(decoded_token[0]['exp']).to eq(decoded_token[0]['iat'] + 60)
29
+ expect(decoded_token[0]['jti']).to eq(uuid)
30
+ end
31
+
32
+ it 'defaults to RS256 algorithm if not specified' do
33
+ uuid = '12345678-1234-5678-1234-567812345678'
34
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
35
+
36
+ jwt_token = described_class.new(client_id, domain_url, client_assertion_signing_key).jwt_token
37
+ decoded_token = JWT.decode(jwt_token, client_assertion_signing_key, true, { algorithm: 'RS256' })
38
+
39
+ expect(decoded_token[0]['iss']).to eq(client_id)
40
+ expect(decoded_token[0]['sub']).to eq(client_id)
41
+ expect(decoded_token[0]['aud']).to eq("#{domain_url}/oauth/token")
42
+ expect(decoded_token[0]['iat']).to be_within(5).of(Time.now.utc.to_i)
43
+ expect(decoded_token[0]['exp']).to eq(decoded_token[0]['iat'] + 60)
44
+ expect(decoded_token[0]['jti']).to eq(uuid)
45
+ end
46
+
47
+ context 'when using ES256 algorithm' do
48
+ let(:client_assertion_signing_key) { OpenSSL::PKey::EC.generate('prime256v1') }
49
+
50
+ it 'generates a valid JWT token' do
51
+ uuid = '12345678-1234-5678-1234-567812345678'
52
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
53
+ jwt_token = described_class.new(client_id,
54
+ domain_url,
55
+ client_assertion_signing_key,
56
+ 'ES256')
57
+ .jwt_token
58
+ decoded_token = JWT.decode(jwt_token, client_assertion_signing_key, true, { algorithm: 'ES256' })
59
+
60
+ expect(decoded_token[0]['iss']).to eq(client_id)
61
+ expect(decoded_token[0]['sub']).to eq(client_id)
62
+ expect(decoded_token[0]['aud']).to eq("#{domain_url}/oauth/token")
63
+ expect(decoded_token[0]['iat']).to be_within(5).of(Time.now.utc.to_i)
64
+ expect(decoded_token[0]['exp']).to eq(decoded_token[0]['iat'] + 60)
65
+ expect(decoded_token[0]['jti']).to eq(uuid)
66
+ end
67
+
68
+ it 'accepts client_assertion_signing_key_token_params as a string' do
69
+ uuid = '12345678-1234-5678-1234-567812345678'
70
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
71
+ jwt_token = described_class.new(client_id,
72
+ domain_url,
73
+ client_assertion_signing_key,
74
+ 'ES256')
75
+ .jwt_token
76
+ decoded_token = JWT.decode(jwt_token, client_assertion_signing_key, true, { algorithm: 'ES256' })
77
+
78
+ expect(decoded_token[0]['iss']).to eq(client_id)
79
+ expect(decoded_token[0]['sub']).to eq(client_id)
80
+ expect(decoded_token[0]['aud']).to eq("#{domain_url}/oauth/token")
81
+ expect(decoded_token[0]['iat']).to be_within(5).of(Time.now.utc.to_i)
82
+ expect(decoded_token[0]['exp']).to eq(decoded_token[0]['iat'] + 60)
83
+ expect(decoded_token[0]['jti']).to eq(uuid)
84
+ end
85
+ end
86
+ end
87
+ end