11using System . Net ;
22using ByteSync . Common . Business . Auth ;
3- using ByteSync . Functions . Constants ;
43using ByteSync . Functions . Helpers ;
54using ByteSync . ServerCommon . Interfaces . Services ;
65using Microsoft . AspNetCore . Authorization ;
76using Microsoft . Azure . Functions . Worker ;
87using Microsoft . Azure . Functions . Worker . Http ;
9- using Microsoft . Extensions . Logging ;
108
119namespace ByteSync . Functions . Http ;
1210
1311public class AuthFunction
1412{
1513 private readonly IAuthService _authService ;
16- private readonly ILogger < AuthFunction > _logger ;
1714
18- public AuthFunction ( IAuthService authService , ILoggerFactory loggerFactory )
15+ public AuthFunction ( IAuthService authService )
1916 {
2017 _authService = authService ;
21- _logger = loggerFactory . CreateLogger < AuthFunction > ( ) ;
2218 }
2319
2420 [ AllowAnonymous ]
2521 [ Function ( "Login" ) ]
2622 public async Task < HttpResponseData > Login ( [ HttpTrigger ( AuthorizationLevel . Anonymous , "post" , "get" , Route = "auth/login" ) ] HttpRequestData req ,
2723 FunctionContext executionContext )
2824 {
29- var response = req . CreateResponse ( ) ;
30- try
31- {
32- var loginData = await FunctionHelper . DeserializeRequestBody < LoginData > ( req ) ;
25+ var loginData = await FunctionHelper . DeserializeRequestBody < LoginData > ( req ) ;
3326
34- var authResult = await _authService . Authenticate ( loginData , GetIpAddress ( req ) ) ;
27+ var authResult = await _authService . Authenticate ( loginData , ExtractIpAddress ( req ) ) ;
3528
36- if ( authResult . IsSuccess )
37- {
38- await response . WriteAsJsonAsync ( authResult , HttpStatusCode . OK ) ;
39- }
40- else
41- {
42- await response . WriteAsJsonAsync ( authResult , HttpStatusCode . Unauthorized ) ;
43- }
29+ var response = req . CreateResponse ( ) ;
30+ if ( authResult . IsSuccess )
31+ {
32+ await response . WriteAsJsonAsync ( authResult , HttpStatusCode . OK ) ;
4433 }
45- catch ( Exception ex )
34+ else
4635 {
47- _logger . LogError ( ex , "Error while logging in" ) ;
48- await response . WriteAsJsonAsync ( new { error = ErrorConstants . INTERNAL_SERVER_ERROR } , HttpStatusCode . InternalServerError ) ;
36+ await response . WriteAsJsonAsync ( authResult , HttpStatusCode . Unauthorized ) ;
4937 }
5038
5139 return response ;
@@ -55,41 +43,31 @@ public async Task<HttpResponseData> Login([HttpTrigger(AuthorizationLevel.Anonym
5543 public async Task < HttpResponseData > RefreshTokens ( [ HttpTrigger ( AuthorizationLevel . Anonymous , "post" , "get" , Route = "auth/refreshTokens" ) ] HttpRequestData req ,
5644 FunctionContext executionContext )
5745 {
58- var response = req . CreateResponse ( ) ;
59- try
60- {
61- var refreshTokensData = await FunctionHelper . DeserializeRequestBody < RefreshTokensData > ( req ) ;
46+ var refreshTokensData = await FunctionHelper . DeserializeRequestBody < RefreshTokensData > ( req ) ;
6247
63- var authResult = await _authService . RefreshTokens ( refreshTokensData , GetIpAddress ( req ) ) ;
48+ var authResult = await _authService . RefreshTokens ( refreshTokensData , ExtractIpAddress ( req ) ) ;
6449
65- if ( authResult . IsSuccess )
66- {
67- await response . WriteAsJsonAsync ( authResult , HttpStatusCode . OK ) ;
68- }
69- else
70- {
71- await response . WriteAsJsonAsync ( authResult , HttpStatusCode . Unauthorized ) ;
72- }
50+ var response = req . CreateResponse ( ) ;
51+ if ( authResult . IsSuccess )
52+ {
53+ await response . WriteAsJsonAsync ( authResult , HttpStatusCode . OK ) ;
7354 }
74- catch ( Exception ex )
55+ else
7556 {
76- _logger . LogError ( ex , "Error while refreshing tokens" ) ;
77- await response . WriteAsJsonAsync ( new { error = ErrorConstants . INTERNAL_SERVER_ERROR } , HttpStatusCode . InternalServerError ) ;
57+ await response . WriteAsJsonAsync ( authResult , HttpStatusCode . Unauthorized ) ;
7858 }
7959
8060 return response ;
8161 }
8262
83- private string GetIpAddress ( HttpRequestData req )
63+ private string ExtractIpAddress ( HttpRequestData req )
8464 {
8565 var headerDictionary = req . Headers . ToDictionary ( x => x . Key . ToLower ( ) , x => x . Value , StringComparer . Ordinal ) ;
8666 var key = "x-forwarded-for" ;
87- if ( headerDictionary . ContainsKey ( key ) )
67+ if ( headerDictionary . TryGetValue ( key , out var headerValues ) )
8868 {
89- IPAddress ? ipAddress = null ;
90- var headerValues = headerDictionary [ key ] ;
91- var ipn = headerValues ? . FirstOrDefault ( ) ? . Split ( new char [ ] { ',' } ) . FirstOrDefault ( ) ? . Split ( new char [ ] { ':' } ) . FirstOrDefault ( ) ;
92- if ( IPAddress . TryParse ( ipn , out ipAddress ) )
69+ var ipn = headerValues . FirstOrDefault ( ) ? . Split ( [ ',' ] ) . FirstOrDefault ( ) ? . Split ( [ ':' ] ) . FirstOrDefault ( ) ;
70+ if ( IPAddress . TryParse ( ipn , out var ipAddress ) )
9371 {
9472 var ipAddressString = ipAddress . ToString ( ) ;
9573 return ipAddressString ;
0 commit comments