Ejemplo n.º 1
0
        public async Task <int> GetUserId(HttpRequest req)
        {
            AuthGUID authGUID = await GetGUIDAsync(req);

            if (!authGUID.Acces || authGUID.GUID == "")
            {
                return(0);
            }

            IAuthorizationRepository authRepository = DIContainer.Instance.GetService <IAuthorizationRepository>();

            return(authRepository.GetUserId(authGUID.GUID, false));
        }
Ejemplo n.º 2
0
        public async Task <AuthResultModel> AuthForDoctorOrPatient(HttpRequest req, int userId)
        {
            AuthGUID authGUID = await GetGUIDAsync(req);

            if (!authGUID.Acces || authGUID.GUID == "")
            {
                return(new AuthResultModel(false, AuthStatusCode.Unauthorized));
            }

            IAuthorizationRepository authRepository = DIContainer.Instance.GetService <IAuthorizationRepository>();

            return(authRepository.HasAcces(userId, authGUID.GUID)
                ? new AuthResultModel(true, AuthStatusCode.Ok)
                : new AuthResultModel(false, AuthStatusCode.Forbidden));
        }
Ejemplo n.º 3
0
        public async Task <AuthResultModel> AuthForDoctor(HttpRequest req, int doctorId)
        {
            AuthGUID authGUID = await GetGUIDAsync(req);

            if (!authGUID.AuthResult.Result)
            {
                return(authGUID.AuthResult);
            }

            IAuthorizationRepository authRepository = DIContainer.Instance.GetService <IAuthorizationRepository>();

            if (authRepository.UserAuth(doctorId, authGUID.GUID, true))
            {
                return(new AuthResultModel(true, AuthStatusCode.Ok));
            }
            return(new AuthResultModel(false, AuthStatusCode.Forbidden));
        }
Ejemplo n.º 4
0
        // Helpers

        private async Task <AuthGUID> GetGUIDAsync(HttpRequest req)
        {
            AuthGUID        authGUID   = new AuthGUID();
            AuthResultModel authResult = new AuthResultModel(false, AuthStatusCode.Unauthorized);

            // Get AuthentificationHeader from request
            AuthenticationHeaderValue.TryParse(req.Headers[HeaderNames.Authorization], out var authHeader);

            if (authHeader == null)
            {
                return new AuthGUID {
                           AuthResult = authResult
                }
            }
            ;

            // Token validation with Auth0 servers
            ClaimsPrincipal claims = await Auth0.ValidateTokenAsync(authHeader);

            if (claims == null)
            {
                return new AuthGUID {
                           AuthResult = authResult
                }
            }
            ;

            // Get Token Guid for Authorization
            string tokenGuid = claims.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;

            authGUID.Acces      = true;
            authGUID.GUID       = tokenGuid;
            authGUID.AuthResult = new AuthResultModel(true, AuthStatusCode.Ok);
            return(authGUID);
        }
    }