1+ using System ;
2+ using System . Security . Cryptography ;
13using System . Threading . Tasks ;
24using Micro . AppRegistration . Api . Models ;
5+ using Micro . AppRegistration . Api . Uuid ;
6+ using Microsoft . AspNetCore . Identity ;
37
48namespace Micro . AppRegistration . Api . CreateApplication
59{
@@ -10,9 +14,45 @@ public interface IAppRegistrationService
1014
1115 public class AppRegistrationService : IAppRegistrationService
1216 {
17+ private readonly IPasswordHasher < Application > _hasher ;
18+ private readonly ICreateApplicationRepository _applicationRepository ;
19+
20+ public AppRegistrationService ( IPasswordHasher < Application > hasher , ICreateApplicationRepository applicationRepository )
21+ {
22+ _hasher = hasher ;
23+ _applicationRepository = applicationRepository ;
24+ }
25+
1326 public async Task < Application > CreateApplication ( CreateApplicationRequest request , string userId )
1427 {
15- throw new System . NotImplementedException ( ) ;
28+ var secret = GenerateApplicationSecret ( ) ;
29+ var hashedSecret = _hasher . HashPassword ( null , secret ) ;
30+ var application = await _applicationRepository . Create ( new Application
31+ {
32+ Approved = false ,
33+ Name = request . Name ,
34+ Secret = hashedSecret ,
35+ User = userId ,
36+ CreatedBy = userId ,
37+ UseDefaultCode = request . UseDefault ,
38+ ShortCode = request . ShortCode
39+ } ) ;
40+ application . Secret = secret ;
41+ return application ;
42+ }
43+
44+ private static string GenerateApplicationSecret ( )
45+ {
46+ return Convert . ToBase64String ( GenerateRandomBytes ( 256 ) ) ;
47+ }
48+
49+ private static byte [ ] GenerateRandomBytes ( int keyBitLength )
50+ {
51+ using var provider = new RNGCryptoServiceProvider ( ) ;
52+ var lengthInByte = keyBitLength / 8 ;
53+ var randomNumber = new byte [ lengthInByte ] ;
54+ provider . GetBytes ( randomNumber ) ;
55+ return randomNumber ;
1656 }
1757 }
1858}
0 commit comments