@@ -86,11 +86,52 @@ public SecurityServiceManager(IPasswordHasher<IdentityUser> passwordHasher,
8686 this . MessagingServiceClient = messagingServiceClient ;
8787 this . IdentityServerTools = identityServerTools ;
8888 }
89+
90+ public async Task < ( Boolean , String ) > ChangePassword ( String userName ,
91+ String currentPassword ,
92+ String newPassword ,
93+ String clientId ,
94+ CancellationToken cancellationToken ) {
95+ // Find the user based on the user name passed in
96+ IdentityUser user = await this . UserManager . FindByNameAsync ( userName ) ;
97+
98+ if ( user == null )
99+ {
100+ // TODO: Redirect to a success page so the user doesnt know if the username is correct or not,
101+ // this prevents giving away info to a potential hacker...
102+ // TODO: maybe log something here...
103+ return ( false , String . Empty ) ;
104+ }
105+
106+ IdentityResult result = await this . UserManager . ChangePasswordAsync ( user , currentPassword , newPassword ) ;
107+
108+ if ( result . Succeeded == false ) {
109+ // Log any errors
110+ Logger . LogWarning ( $ "Errors during password change for user [{ userName } and Client [{ clientId } ]") ;
111+ foreach ( IdentityError identityError in result . Errors )
112+ {
113+ Logger . LogWarning ( $ "Code { identityError . Code } Description { identityError . Description } ") ;
114+ }
115+ }
116+
117+ // build the redirect uri
118+ Duende . IdentityServer . EntityFramework . Entities . Client client = await this . ConfigurationDbContext . Clients . SingleOrDefaultAsync ( c => c . ClientId == clientId ) ;
119+
120+ if ( client == null )
121+ {
122+ Logger . LogWarning ( $ "Client not found for clientId { clientId } ") ;
123+ // TODO: need to redirect somewhere...
124+ return ( false , String . Empty ) ;
125+ }
126+
127+ Logger . LogWarning ( $ "Client uri { client . ClientUri } ") ;
128+ return ( true , client . ClientUri ) ;
129+ }
89130
90131 public async Task ProcessPasswordResetRequest ( String username ,
91- String emailAddress ,
92- String clientId ,
93- CancellationToken cancellationToken ) {
132+ String emailAddress ,
133+ String clientId ,
134+ CancellationToken cancellationToken ) {
94135 // Find the user based on the user name passed in
95136 IdentityUser user = await this . UserManager . FindByNameAsync ( username ) ;
96137
@@ -101,8 +142,7 @@ public async Task ProcessPasswordResetRequest(String username,
101142 return ;
102143 }
103144
104- // TODO: User has been found so send an email with reset details
105- // TODO: For now we will write this to a text file
145+ // User has been found so send an email with reset details
106146 String resetToken = await this . UserManager . GeneratePasswordResetTokenAsync ( user ) ;
107147 resetToken = UrlEncoder . Default . Encode ( resetToken ) ;
108148 String baseAddress = ConfigurationReader . GetValue ( "ServiceOptions" , "PublicOrigin" ) ;
@@ -202,7 +242,7 @@ public async Task<String> ProcessPasswordResetConfirmation(String username,
202242 }
203243 }
204244
205- // TODO: build the redirect uri
245+ // build the redirect uri
206246 Duende . IdentityServer . EntityFramework . Entities . Client client = await this . ConfigurationDbContext . Clients . SingleOrDefaultAsync ( c => c . ClientId == clientId ) ;
207247
208248 if ( client == null ) {
0 commit comments