3
3
4
4
using System . Globalization ;
5
5
using System . Linq ;
6
+ using System . Text ;
6
7
using Microsoft . Extensions . CommandLineUtils ;
7
8
using Microsoft . Extensions . Tools . Internal ;
8
9
@@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools;
11
12
internal sealed class CreateCommand
12
13
{
13
14
private static readonly string [ ] _dateTimeFormats = new [ ] {
14
- "yyyy-MM-dd" , "yyyy-MM-dd HH:mm" , "yyyy/MM/dd" , "yyyy/MM/dd HH:mm" } ;
15
+ "yyyy-MM-dd" , "yyyy-MM-dd HH:mm" , "yyyy/MM/dd" , "yyyy/MM/dd HH:mm" , "yyyy-MM-ddTHH:mm:ss.fffffffzzz" } ;
15
16
private static readonly string [ ] _timeSpanFormats = new [ ] {
16
17
@"d\dh\hm\ms\s" , @"d\dh\hm\m" , @"d\dh\h" , @"d\d" ,
17
18
@"h\hm\ms\s" , @"h\hm\m" , @"h\h" ,
@@ -32,7 +33,7 @@ public static void Register(ProjectCommandLineApplication app)
32
33
) ;
33
34
34
35
var nameOption = cmd . Option (
35
- "--name" ,
36
+ "-n|- -name" ,
36
37
Resources . CreateCommand_NameOption_Description ,
37
38
CommandOptionType . SingleValue ) ;
38
39
@@ -80,20 +81,20 @@ public static void Register(ProjectCommandLineApplication app)
80
81
81
82
cmd . OnExecute ( ( ) =>
82
83
{
83
- var ( options , isValid ) = ValidateArguments (
84
+ var ( options , isValid , optionsString ) = ValidateArguments (
84
85
cmd . Reporter , cmd . ProjectOption , schemeNameOption , nameOption , audienceOption , issuerOption , notBeforeOption , expiresOnOption , validForOption , rolesOption , scopesOption , claimsOption ) ;
85
86
86
87
if ( ! isValid )
87
88
{
88
89
return 1 ;
89
90
}
90
91
91
- return Execute ( cmd . Reporter , cmd . ProjectOption . Value ( ) , options ) ;
92
+ return Execute ( cmd . Reporter , cmd . ProjectOption . Value ( ) , options , optionsString ) ;
92
93
} ) ;
93
94
} ) ;
94
95
}
95
96
96
- private static ( JwtCreatorOptions , bool ) ValidateArguments (
97
+ private static ( JwtCreatorOptions , bool , string ) ValidateArguments (
97
98
IReporter reporter ,
98
99
CommandOption projectOption ,
99
100
CommandOption schemeNameOption ,
@@ -109,16 +110,22 @@ private static (JwtCreatorOptions, bool) ValidateArguments(
109
110
{
110
111
var isValid = true ;
111
112
var project = DevJwtCliHelpers . GetProject ( projectOption . Value ( ) ) ;
113
+
112
114
var scheme = schemeNameOption . HasValue ( ) ? schemeNameOption . Value ( ) : "Bearer" ;
115
+ var optionsString = schemeNameOption . HasValue ( ) ? $ "{ Resources . JwtPrint_Scheme } : { scheme } { Environment . NewLine } " : string . Empty ;
116
+
113
117
var name = nameOption . HasValue ( ) ? nameOption . Value ( ) : Environment . UserName ;
118
+ optionsString += $ "{ Resources . JwtPrint_Name } : { name } { Environment . NewLine } ";
114
119
115
120
var audience = audienceOption . HasValue ( ) ? audienceOption . Values : DevJwtCliHelpers . GetAudienceCandidatesFromLaunchSettings ( project ) . ToList ( ) ;
121
+ optionsString += audienceOption . HasValue ( ) ? $ "{ Resources . JwtPrint_Audiences } : { audience } { Environment . NewLine } " : string . Empty ;
116
122
if ( audience is null )
117
123
{
118
124
reporter . Error ( Resources . CreateCommand_NoAudience_Error ) ;
119
125
isValid = false ;
120
126
}
121
127
var issuer = issuerOption . HasValue ( ) ? issuerOption . Value ( ) : DevJwtsDefaults . Issuer ;
128
+ optionsString += issuerOption . HasValue ( ) ? $ "{ Resources . JwtPrint_Issuer } : { issuer } { Environment . NewLine } " : string . Empty ;
122
129
123
130
var notBefore = DateTime . UtcNow ;
124
131
if ( notBeforeOption . HasValue ( ) )
@@ -128,6 +135,7 @@ private static (JwtCreatorOptions, bool) ValidateArguments(
128
135
reporter . Error ( Resources . FormatCreateCommand_InvalidDate_Error ( "--not-before" ) ) ;
129
136
isValid = false ;
130
137
}
138
+ optionsString += $ "{ Resources . JwtPrint_NotBefore } : { notBefore : O} { Environment . NewLine } ";
131
139
}
132
140
133
141
var expiresOn = notBefore . AddMonths ( 3 ) ;
@@ -138,6 +146,17 @@ private static (JwtCreatorOptions, bool) ValidateArguments(
138
146
reporter . Error ( Resources . FormatCreateCommand_InvalidDate_Error ( "--expires-on" ) ) ;
139
147
isValid = false ;
140
148
}
149
+
150
+ if ( validForOption . HasValue ( ) )
151
+ {
152
+ reporter . Error ( Resources . CreateCommand_InvalidExpiresOn_Error ) ;
153
+ isValid = false ;
154
+ }
155
+ else
156
+ {
157
+ optionsString += $ "{ Resources . JwtPrint_ExpiresOn } : { expiresOn : O} { Environment . NewLine } ";
158
+ }
159
+
141
160
}
142
161
143
162
if ( validForOption . HasValue ( ) )
@@ -147,10 +166,23 @@ private static (JwtCreatorOptions, bool) ValidateArguments(
147
166
reporter . Error ( Resources . FormatCreateCommand_InvalidPeriod_Error ( "--valid-for" ) ) ;
148
167
}
149
168
expiresOn = notBefore . Add ( validForValue ) ;
169
+
170
+ if ( expiresOnOption . HasValue ( ) )
171
+ {
172
+ reporter . Error ( Resources . CreateCommand_InvalidExpiresOn_Error ) ;
173
+ isValid = false ;
174
+ }
175
+ else
176
+ {
177
+ optionsString += $ "{ Resources . JwtPrint_ExpiresOn } : { expiresOn : O} { Environment . NewLine } ";
178
+ }
150
179
}
151
180
152
181
var roles = rolesOption . HasValue ( ) ? rolesOption . Values : new List < string > ( ) ;
182
+ optionsString += rolesOption . HasValue ( ) ? $ "{ Resources . JwtPrint_Roles } : [{ string . Join ( ", " , roles ) } ]{ Environment . NewLine } " : string . Empty ;
183
+
153
184
var scopes = scopesOption . HasValue ( ) ? scopesOption . Values : new List < string > ( ) ;
185
+ optionsString += scopesOption . HasValue ( ) ? $ "{ Resources . JwtPrint_Scopes } : { string . Join ( ", " , scopes ) } { Environment . NewLine } " : string . Empty ;
154
186
155
187
var claims = new Dictionary < string , string > ( ) ;
156
188
if ( claimsOption . HasValue ( ) )
@@ -160,9 +192,13 @@ private static (JwtCreatorOptions, bool) ValidateArguments(
160
192
reporter . Error ( Resources . CreateCommand_InvalidClaims_Error ) ;
161
193
isValid = false ;
162
194
}
195
+ optionsString += $ "{ Resources . JwtPrint_CustomClaims } : [{ string . Join ( ", " , claims . Select ( kvp => $ "{ kvp . Key } ={ kvp . Value } ") ) } ]{ Environment . NewLine } ";
163
196
}
164
197
165
- return ( new JwtCreatorOptions ( scheme , name , audience , issuer , notBefore , expiresOn , roles , scopes , claims ) , isValid ) ;
198
+ return (
199
+ new JwtCreatorOptions ( scheme , name , audience , issuer , notBefore , expiresOn , roles , scopes , claims ) ,
200
+ isValid ,
201
+ optionsString ) ;
166
202
167
203
static bool ParseDate ( string datetime , out DateTime parsedDateTime ) =>
168
204
DateTime . TryParseExact ( datetime , _dateTimeFormats , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal | DateTimeStyles . AdjustToUniversal , out parsedDateTime ) ;
@@ -171,7 +207,8 @@ static bool ParseDate(string datetime, out DateTime parsedDateTime) =>
171
207
private static int Execute (
172
208
IReporter reporter ,
173
209
string projectPath ,
174
- JwtCreatorOptions options )
210
+ JwtCreatorOptions options ,
211
+ string optionsString )
175
212
{
176
213
if ( ! DevJwtCliHelpers . GetProjectAndSecretsId ( projectPath , reporter , out var project , out var userSecretsId ) )
177
214
{
@@ -196,6 +233,8 @@ private static int Execute(
196
233
settingsToWrite . Save ( appsettingsFilePath ) ;
197
234
198
235
reporter . Output ( Resources . FormatCreateCommand_Confirmed ( jwtToken . Id ) ) ;
236
+ reporter . Output ( optionsString ) ;
237
+ reporter . Output ( $ "{ Resources . JwtPrint_Token } : { jwt . Token } ") ;
199
238
200
239
return 0 ;
201
240
}
0 commit comments