Revision | 10 (tree) |
---|---|
Time | 2020-10-05 14:18:39 |
Author | ![]() |
update extended jwt poc
@@ -1,163 +0,0 @@ | ||
1 | -using Grpc.Core; | |
2 | -using SL.SessionJwt.Service; | |
3 | -using System; | |
4 | -using System.Runtime.CompilerServices; | |
5 | -using System.Threading.Tasks; | |
6 | - | |
7 | -namespace SL.SessionJwt.ConsoleClient | |
8 | -{ | |
9 | - public class OutdatedTookie | |
10 | - { | |
11 | - public async Task Start() | |
12 | - { | |
13 | - WriteLine($"=== {nameof(OutdatedTookie)} starts. ==="); | |
14 | - | |
15 | - var user = await LogonAndSayHello(); | |
16 | - await Wait1MinuteForTokenTimeout(); | |
17 | - await SayHelloAndFail(user); | |
18 | - await RenewTokenAndSayHello(user); | |
19 | - await Logout(user, true); | |
20 | - await SayHelloAndFail(user); | |
21 | - await RenewAndFailBecauseExplicitLogout(user); | |
22 | - | |
23 | - user = await LogonAndSayHello(); | |
24 | - await Wait2MinutesForTokenTimeoutNotRenewable(); | |
25 | - await RenewAndFailBecauseNotRenewable(user); | |
26 | - await Logout(user); | |
27 | - | |
28 | - WriteLine($"=== {nameof(OutdatedTookie)} passed. ==={Environment.NewLine}"); | |
29 | - } | |
30 | - | |
31 | - public async Task<UserProxy> LogonAndSayHello() | |
32 | - { | |
33 | - var user = new UserProxy("User1"); | |
34 | - try | |
35 | - { | |
36 | - await user.LogonAsync(); | |
37 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
38 | - WriteLine("passed."); | |
39 | - return user; | |
40 | - } | |
41 | - catch (RpcException exception) | |
42 | - { | |
43 | - if (exception.StatusCode != StatusCode.InvalidArgument) | |
44 | - WriteLine($"RpcException {exception.StatusCode}"); | |
45 | - throw; | |
46 | - } | |
47 | - catch (Exception exception) | |
48 | - { | |
49 | - WriteLine($"Exception {exception.Message}"); | |
50 | - throw; | |
51 | - } | |
52 | - } | |
53 | - | |
54 | - public static async Task Wait1MinuteForTokenTimeout() | |
55 | - { | |
56 | - Console.WriteLine("Wait a minute."); | |
57 | - await Task.Delay(61000); | |
58 | - } | |
59 | - | |
60 | - public static async Task Wait2MinutesForTokenTimeoutNotRenewable() | |
61 | - { | |
62 | - Console.WriteLine("Wait 2 minutes."); | |
63 | - await Task.Delay(121000); | |
64 | - } | |
65 | - | |
66 | - public static async Task SayHelloAndFail(UserProxy user) | |
67 | - { | |
68 | - try | |
69 | - { | |
70 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
71 | - } | |
72 | - catch (RpcException) | |
73 | - { | |
74 | - WriteLine("passed."); | |
75 | - return; | |
76 | - } | |
77 | - catch (Exception exception) | |
78 | - { | |
79 | - WriteLine($"{nameof(SayHelloAndFail)} Unexpected Exception {exception.Message}"); | |
80 | - throw; | |
81 | - } | |
82 | - throw new Exception("This should go wrong."); | |
83 | - } | |
84 | - | |
85 | - public static async Task RenewTokenAndSayHello(UserProxy user) | |
86 | - { | |
87 | - try | |
88 | - { | |
89 | - await user.RenewAsync(); | |
90 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
91 | - WriteLine("passed."); | |
92 | - } | |
93 | - catch (RpcException exception) | |
94 | - { | |
95 | - if (exception.StatusCode != StatusCode.InvalidArgument) | |
96 | - WriteLine($"RpcException {exception.StatusCode}"); | |
97 | - throw; | |
98 | - } | |
99 | - catch (Exception exception) | |
100 | - { | |
101 | - WriteLine($"Exception {exception.Message}"); | |
102 | - throw; | |
103 | - } | |
104 | - } | |
105 | - | |
106 | - public static async Task Logout(UserProxy user, bool keepToken = false) | |
107 | - { | |
108 | - try | |
109 | - { | |
110 | - await user.LogoutAsync(keepToken); | |
111 | - WriteLine("passed."); | |
112 | - } | |
113 | - catch (Exception exception) | |
114 | - { | |
115 | - Console.WriteLine($"Logout failed. {exception.Message}"); | |
116 | - throw; | |
117 | - } | |
118 | - } | |
119 | - | |
120 | - public static async Task RenewAndFailBecauseExplicitLogout(UserProxy user) | |
121 | - { | |
122 | - try | |
123 | - { | |
124 | - await user.RenewAsync(); | |
125 | - } | |
126 | - catch (RpcException) | |
127 | - { | |
128 | - WriteLine("passed."); | |
129 | - return; | |
130 | - } | |
131 | - catch (Exception exception) | |
132 | - { | |
133 | - WriteLine($"{nameof(RenewAndFailBecauseExplicitLogout)} Unexpected Exception {exception.Message}"); | |
134 | - throw; | |
135 | - } | |
136 | - throw new Exception("This should go wrong."); | |
137 | - } | |
138 | - | |
139 | - public static async Task RenewAndFailBecauseNotRenewable(UserProxy user) | |
140 | - { | |
141 | - try | |
142 | - { | |
143 | - await user.RenewAsync(); | |
144 | - } | |
145 | - catch (RpcException) | |
146 | - { | |
147 | - WriteLine("passed."); | |
148 | - return; | |
149 | - } | |
150 | - catch (Exception exception) | |
151 | - { | |
152 | - WriteLine($"{nameof(RenewAndFailBecauseNotRenewable)} Unexpected Exception {exception.Message}"); | |
153 | - throw; | |
154 | - } | |
155 | - throw new Exception("This should go wrong."); | |
156 | - } | |
157 | - | |
158 | - static void WriteLine(string message, [CallerMemberName] string caller = "") | |
159 | - { | |
160 | - Console.WriteLine($"{caller}: {message}"); | |
161 | - } | |
162 | - } | |
163 | -} |
@@ -36,18 +36,21 @@ | ||
36 | 36 | { |
37 | 37 | WriteLine($"Exception {exception.Message}"); |
38 | 38 | } |
39 | + user?.Dispose(); | |
39 | 40 | } |
40 | 41 | |
41 | 42 | private async Task LogonTwice() |
42 | 43 | { |
43 | 44 | try |
44 | - { | |
45 | + { | |
45 | 46 | // second logon overrides old session |
46 | 47 | // and should not throw an error |
47 | - var user = new UserProxy("User1"); | |
48 | - await user.LogonAsync(); | |
49 | - await user.LogonAsync(); | |
50 | - WriteLine("passed"); | |
48 | + using (var user = new UserProxy("User1")) | |
49 | + { | |
50 | + await user.LogonAsync(); | |
51 | + await user.LogonAsync(); | |
52 | + WriteLine("passed"); | |
53 | + } | |
51 | 54 | } |
52 | 55 | catch (Exception exception) |
53 | 56 | { |
@@ -60,13 +63,15 @@ | ||
60 | 63 | try |
61 | 64 | { |
62 | 65 | // second logout should does just nothing |
63 | - var user = new UserProxy("User1"); | |
64 | - await user.LogonAsync(); | |
65 | - var logout1Result = await user.LogoutAsync(); | |
66 | - var logout2Result = await user.LogoutAsync(); | |
67 | - if (!logout1Result || logout2Result) | |
68 | - throw new Exception($"logout1Result is{logout1Result} and logout2Result is {logout2Result}."); | |
69 | - WriteLine("passed"); | |
66 | + using (var user = new UserProxy("User1")) | |
67 | + { | |
68 | + await user.LogonAsync(); | |
69 | + var logout1Result = await user.LogoutAsync(); | |
70 | + var logout2Result = await user.LogoutAsync(); | |
71 | + if (!logout1Result || logout2Result) | |
72 | + throw new Exception($"logout1Result is{logout1Result} and logout2Result is {logout2Result}."); | |
73 | + WriteLine("passed"); | |
74 | + } | |
70 | 75 | } |
71 | 76 | catch (Exception exception) |
72 | 77 | { |
@@ -0,0 +1,167 @@ | ||
1 | +using Grpc.Core; | |
2 | +using SL.SessionJwt.Service; | |
3 | +using System; | |
4 | +using System.Runtime.CompilerServices; | |
5 | +using System.Threading.Tasks; | |
6 | + | |
7 | +namespace SL.SessionJwt.ConsoleClient | |
8 | +{ | |
9 | + public class OutdatedTookie | |
10 | + { | |
11 | + public async Task Start() | |
12 | + { | |
13 | + WriteLine($"=== {nameof(OutdatedTookie)} starts. ==="); | |
14 | + | |
15 | + using (var user = await LogonAndSayHello()) | |
16 | + { | |
17 | + await Wait1MinuteForTokenTimeout(); | |
18 | + await SayHelloAndFail(user); | |
19 | + await RenewTokenAndSayHello(user); | |
20 | + await Logout(user, true); | |
21 | + await SayHelloAndFail(user); | |
22 | + await RenewAndFailBecauseExplicitLogout(user); | |
23 | + } | |
24 | + | |
25 | + using (var user = await LogonAndSayHello()) | |
26 | + { | |
27 | + await Wait2MinutesForTokenTimeoutNotRenewable(); | |
28 | + await RenewAndFailBecauseNotRenewable(user); | |
29 | + await Logout(user); | |
30 | + } | |
31 | + | |
32 | + WriteLine($"=== {nameof(OutdatedTookie)} passed. ==={Environment.NewLine}"); | |
33 | + } | |
34 | + | |
35 | + public async Task<UserProxy> LogonAndSayHello() | |
36 | + { | |
37 | + var user = new UserProxy("User1"); | |
38 | + try | |
39 | + { | |
40 | + await user.LogonAsync(); | |
41 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
42 | + WriteLine("passed."); | |
43 | + return user; | |
44 | + } | |
45 | + catch (RpcException exception) | |
46 | + { | |
47 | + if (exception.StatusCode != StatusCode.InvalidArgument) | |
48 | + WriteLine($"RpcException {exception.StatusCode}"); | |
49 | + throw; | |
50 | + } | |
51 | + catch (Exception exception) | |
52 | + { | |
53 | + WriteLine($"Exception {exception.Message}"); | |
54 | + throw; | |
55 | + } | |
56 | + } | |
57 | + | |
58 | + public static async Task Wait1MinuteForTokenTimeout() | |
59 | + { | |
60 | + Console.WriteLine("Wait a minute."); | |
61 | + await Task.Delay(61000); | |
62 | + } | |
63 | + | |
64 | + public static async Task Wait2MinutesForTokenTimeoutNotRenewable() | |
65 | + { | |
66 | + Console.WriteLine("Wait 2 minutes."); | |
67 | + await Task.Delay(121000); | |
68 | + } | |
69 | + | |
70 | + public static async Task SayHelloAndFail(UserProxy user) | |
71 | + { | |
72 | + try | |
73 | + { | |
74 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
75 | + } | |
76 | + catch (RpcException) | |
77 | + { | |
78 | + WriteLine("passed."); | |
79 | + return; | |
80 | + } | |
81 | + catch (Exception exception) | |
82 | + { | |
83 | + WriteLine($"{nameof(SayHelloAndFail)} Unexpected Exception {exception.Message}"); | |
84 | + throw; | |
85 | + } | |
86 | + throw new Exception("This should go wrong."); | |
87 | + } | |
88 | + | |
89 | + public static async Task RenewTokenAndSayHello(UserProxy user) | |
90 | + { | |
91 | + try | |
92 | + { | |
93 | + await user.RenewAsync(); | |
94 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(OutdatedTookie) }); | |
95 | + WriteLine("passed."); | |
96 | + } | |
97 | + catch (RpcException exception) | |
98 | + { | |
99 | + if (exception.StatusCode != StatusCode.InvalidArgument) | |
100 | + WriteLine($"RpcException {exception.StatusCode}"); | |
101 | + throw; | |
102 | + } | |
103 | + catch (Exception exception) | |
104 | + { | |
105 | + WriteLine($"Exception {exception.Message}"); | |
106 | + throw; | |
107 | + } | |
108 | + } | |
109 | + | |
110 | + public static async Task Logout(UserProxy user, bool keepToken = false) | |
111 | + { | |
112 | + try | |
113 | + { | |
114 | + await user.LogoutAsync(keepToken); | |
115 | + WriteLine("passed."); | |
116 | + } | |
117 | + catch (Exception exception) | |
118 | + { | |
119 | + Console.WriteLine($"Logout failed. {exception.Message}"); | |
120 | + throw; | |
121 | + } | |
122 | + } | |
123 | + | |
124 | + public static async Task RenewAndFailBecauseExplicitLogout(UserProxy user) | |
125 | + { | |
126 | + try | |
127 | + { | |
128 | + await user.RenewAsync(); | |
129 | + } | |
130 | + catch (RpcException) | |
131 | + { | |
132 | + WriteLine("passed."); | |
133 | + return; | |
134 | + } | |
135 | + catch (Exception exception) | |
136 | + { | |
137 | + WriteLine($"{nameof(RenewAndFailBecauseExplicitLogout)} Unexpected Exception {exception.Message}"); | |
138 | + throw; | |
139 | + } | |
140 | + throw new Exception("This should go wrong."); | |
141 | + } | |
142 | + | |
143 | + public static async Task RenewAndFailBecauseNotRenewable(UserProxy user) | |
144 | + { | |
145 | + try | |
146 | + { | |
147 | + await user.RenewAsync(); | |
148 | + } | |
149 | + catch (RpcException) | |
150 | + { | |
151 | + WriteLine("passed."); | |
152 | + return; | |
153 | + } | |
154 | + catch (Exception exception) | |
155 | + { | |
156 | + WriteLine($"{nameof(RenewAndFailBecauseNotRenewable)} Unexpected Exception {exception.Message}"); | |
157 | + throw; | |
158 | + } | |
159 | + throw new Exception("This should go wrong."); | |
160 | + } | |
161 | + | |
162 | + static void WriteLine(string message, [CallerMemberName] string caller = "") | |
163 | + { | |
164 | + Console.WriteLine($"{caller}: {message}"); | |
165 | + } | |
166 | + } | |
167 | +} |
@@ -20,14 +20,16 @@ | ||
20 | 20 | { |
21 | 21 | try |
22 | 22 | { |
23 | - var user = new UserProxy("User1"); | |
24 | - user.Invoker.WriteTokenToConsole = true; | |
25 | - await user.LogonAsync(); | |
26 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
27 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
28 | - await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
29 | - await user.LogoutAsync(); | |
30 | - WriteLine("passed"); | |
23 | + using (var user = new UserProxy("User1")) | |
24 | + { | |
25 | + user.Invoker.WriteTokenToConsole = true; | |
26 | + await user.LogonAsync(); | |
27 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
28 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
29 | + await user.Client<Greeter.GreeterClient>().SayHelloAsync(new HelloRequest { Name = nameof(TestSayHello) }); | |
30 | + await user.LogoutAsync(); | |
31 | + WriteLine("passed"); | |
32 | + } | |
31 | 33 | } |
32 | 34 | catch (Exception exception) |
33 | 35 | { |
@@ -101,6 +101,7 @@ | ||
101 | 101 | |
102 | 102 | return true; |
103 | 103 | } |
104 | + | |
104 | 105 | internal virtual bool IsValidLifetimeOrExpiresWithinRenewPeriod(JwtSecurityToken jwtToken) |
105 | 106 | { |
106 | 107 | var result = default(bool); |
@@ -1,5 +1,4 @@ | ||
1 | 1 | using System; |
2 | -using System.Collections.Generic; | |
3 | 2 | using System.IdentityModel.Tokens.Jwt; |
4 | 3 | using System.Linq; |
5 | 4 | using System.Threading.Tasks; |
@@ -41,4 +40,4 @@ | ||
41 | 40 | } |
42 | 41 | } |
43 | 42 | } |
44 | -} | |
\ No newline at end of file | ||
43 | +} |
@@ -1,5 +1,4 @@ | ||
1 | 1 | using System; |
2 | -using Auth = Microsoft.AspNetCore.Authentication; | |
3 | 2 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
4 | 3 | using Microsoft.AspNetCore.Builder; |
5 | 4 | using Microsoft.AspNetCore.Hosting; |
@@ -6,9 +5,7 @@ | ||
6 | 5 | using Microsoft.AspNetCore.Http; |
7 | 6 | using Microsoft.Extensions.DependencyInjection; |
8 | 7 | using Microsoft.Extensions.Hosting; |
9 | -using Microsoft.IdentityModel.Tokens; | |
10 | 8 | using SL.SessionJwt.Service.Services; |
11 | -using Microsoft.AspNetCore.Authorization; | |
12 | 9 | using Microsoft.IdentityModel.JsonWebTokens; |
13 | 10 | using System.Security.Claims; |
14 | 11 | using System.Linq; |
@@ -24,14 +21,6 @@ | ||
24 | 21 | services.AddSingleton<JwtSessionValidator>(); |
25 | 22 | services.AddSingleton<JwtTokenEvents>(); |
26 | 23 | |
27 | - //services.AddCors(o => o.AddPolicy("MyPolicy", builder => | |
28 | - //{ | |
29 | - // builder.AllowAnyOrigin() | |
30 | - // .AllowAnyMethod() | |
31 | - // .AllowAnyHeader() | |
32 | - // .WithExposedHeaders("Token-Expired"); | |
33 | - //})); | |
34 | - | |
35 | 24 | services.AddGrpc(); |
36 | 25 | |
37 | 26 | services.AddAuthentication(x => |
@@ -66,7 +55,6 @@ | ||
66 | 55 | app.UseDeveloperExceptionPage(); |
67 | 56 | } |
68 | 57 | |
69 | - //app.UseCors("MyPolicy"); | |
70 | 58 | app.UseRouting(); |
71 | 59 | app.UseAuthentication(); |
72 | 60 | app.UseAuthorization(); |