• R/O
  • SSH
  • HTTPS

grpcdotnet: Commit


Commit MetaInfo

Revision10 (tree)
Time2020-10-05 14:18:39
Authorsebastiandotnet

Log Message

update extended jwt poc

Change Summary

Incremental Difference

--- SessionJwt/SL.SessionJwt.ConsoleClient/OutdatedTookie.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.ConsoleClient/OutdatedTookie.cs (nonexistent)
@@ -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-}
--- SessionJwt/SL.SessionJwt.ConsoleClient/TestLogonLogout.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.ConsoleClient/TestLogonLogout.cs (revision 10)
@@ -36,18 +36,21 @@
3636 {
3737 WriteLine($"Exception {exception.Message}");
3838 }
39+ user?.Dispose();
3940 }
4041
4142 private async Task LogonTwice()
4243 {
4344 try
44- {
45+ {
4546 // second logon overrides old session
4647 // 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+ }
5154 }
5255 catch (Exception exception)
5356 {
@@ -60,13 +63,15 @@
6063 try
6164 {
6265 // 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+ }
7075 }
7176 catch (Exception exception)
7277 {
--- SessionJwt/SL.SessionJwt.ConsoleClient/TestOutdatedTookie.cs (nonexistent)
+++ SessionJwt/SL.SessionJwt.ConsoleClient/TestOutdatedTookie.cs (revision 10)
@@ -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+}
--- SessionJwt/SL.SessionJwt.ConsoleClient/TestSayHello.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.ConsoleClient/TestSayHello.cs (revision 10)
@@ -20,14 +20,16 @@
2020 {
2121 try
2222 {
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+ }
3133 }
3234 catch (Exception exception)
3335 {
--- SessionJwt/SL.SessionJwt.Service/JwtExtensions/JwtSigningTokenHandler.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.Service/JwtExtensions/JwtSigningTokenHandler.cs (revision 10)
@@ -101,6 +101,7 @@
101101
102102 return true;
103103 }
104+
104105 internal virtual bool IsValidLifetimeOrExpiresWithinRenewPeriod(JwtSecurityToken jwtToken)
105106 {
106107 var result = default(bool);
--- SessionJwt/SL.SessionJwt.Service/JwtExtensions/JwtTokenEvents.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.Service/JwtExtensions/JwtTokenEvents.cs (revision 10)
@@ -1,5 +1,4 @@
11 using System;
2-using System.Collections.Generic;
32 using System.IdentityModel.Tokens.Jwt;
43 using System.Linq;
54 using System.Threading.Tasks;
@@ -41,4 +40,4 @@
4140 }
4241 }
4342 }
44-}
\ No newline at end of file
43+}
--- SessionJwt/SL.SessionJwt.Service/Startup.cs (revision 9)
+++ SessionJwt/SL.SessionJwt.Service/Startup.cs (revision 10)
@@ -1,5 +1,4 @@
11 using System;
2-using Auth = Microsoft.AspNetCore.Authentication;
32 using Microsoft.AspNetCore.Authentication.JwtBearer;
43 using Microsoft.AspNetCore.Builder;
54 using Microsoft.AspNetCore.Hosting;
@@ -6,9 +5,7 @@
65 using Microsoft.AspNetCore.Http;
76 using Microsoft.Extensions.DependencyInjection;
87 using Microsoft.Extensions.Hosting;
9-using Microsoft.IdentityModel.Tokens;
108 using SL.SessionJwt.Service.Services;
11-using Microsoft.AspNetCore.Authorization;
129 using Microsoft.IdentityModel.JsonWebTokens;
1310 using System.Security.Claims;
1411 using System.Linq;
@@ -24,14 +21,6 @@
2421 services.AddSingleton<JwtSessionValidator>();
2522 services.AddSingleton<JwtTokenEvents>();
2623
27- //services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
28- //{
29- // builder.AllowAnyOrigin()
30- // .AllowAnyMethod()
31- // .AllowAnyHeader()
32- // .WithExposedHeaders("Token-Expired");
33- //}));
34-
3524 services.AddGrpc();
3625
3726 services.AddAuthentication(x =>
@@ -66,7 +55,6 @@
6655 app.UseDeveloperExceptionPage();
6756 }
6857
69- //app.UseCors("MyPolicy");
7058 app.UseRouting();
7159 app.UseAuthentication();
7260 app.UseAuthorization();
Show on old repository browser