Beispiel #1
0
        public async Task <IHttpActionResult> GetLocalExternalAccessToken(string provider, string externalAccessToken, string client_id, string client_secret = "")
        {
            if (string.IsNullOrWhiteSpace(provider) || string.IsNullOrWhiteSpace(externalAccessToken))
            {
                return(BadRequest("Provider or external access token is not sent"));
            }
            KeyValuePair <string, Func <ITokenInfo> > searchExternalProvider;

            try
            {
                searchExternalProvider = WebApiApplication.TokenInfoList.First(x => x.Key == provider);
            }
            catch (InvalidOperationException)
            {
                ModelState.AddModelError("Provider", GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST);
                return(BadRequest(ModelState));
            }
            var instance = searchExternalProvider.Value.Invoke();
            var verifiedTokenExternal = await instance.Get(externalAccessToken);

            if (verifiedTokenExternal == null)
            {
                ModelState.AddModelError(GenericNames.AUTHENTICATION_EXTERNAL_LOGIN, GenericError.INVALID_GIVEN_PARAMETER);
                return(BadRequest(ModelState));
            }
            Client userClient = await Manager.FindAsync(new UserLoginInfo(provider, verifiedTokenExternal.user_id));

            if (userClient == null)
            {
                ModelState.AddModelError("Client", GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST);
                return(BadRequest(ModelState));
            }
            Service service = null;

            try
            {
                service = await AuthenticationTools.GetServiceActiveAndExists(Request.GetOwinContext().Get <ManahostManagerDAL>(), client_id, client_secret);
            }
            catch (AuthenticationToolsException e)
            {
                ModelState.AddModelError(e.Key, e.Value);
                return(BadRequest(ModelState));
            }
            var ticket = AuthenticationTools.GenerateTicket(OAuthDefaults.AuthenticationType, client_id, userClient);
            var accessTokenExternal = AuthenticationTools.GenerateToken(ticket, userClient);

            return(OkWithHeader(accessTokenExternal, new Dictionary <string, string>()
            {
                { GenericNames.OWIN_CONTEXT_CORS_HEADER, service.AllowedOrigin }
            }));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> PostRegisterExternel(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            KeyValuePair <string, Func <ITokenInfo> > searchExternalProvider;

            try
            {
                searchExternalProvider = WebApiApplication.TokenInfoList.First(x => x.Key == model.Provider);
            }
            catch (InvalidOperationException)
            {
                ModelState.AddModelError("Provider", GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST);
                return(BadRequest(ModelState));
            }
            var instance    = searchExternalProvider.Value.Invoke();
            var parsedToken = await instance.Get(model.ExternalAccessToken);

            if (parsedToken == null)
            {
                ModelState.AddModelError(GenericNames.AUTHENTICATION_EXTERNAL_LOGIN, GenericError.INVALID_GIVEN_PARAMETER);
                return(BadRequest(ModelState));
            }
            Client user = await Manager.FindAsync(new UserLoginInfo(model.Provider, parsedToken.user_id));

            if (user != null)
            {
                ModelState.AddModelError("Client", GenericError.ALREADY_EXISTS);
                return(BadRequest(ModelState));
            }

            var informationUser = await instance.GetUserInfo(model.ExternalAccessToken);

            var client       = Factory.Create(informationUser);
            var resultClient = await Manager.CreateAsync(client);

            if (!resultClient.Succeeded)
            {
                //TODO ERROR
                return(BadRequest());
            }
            var ResultLoginClient = await Manager.AddLoginAsync(client.Id, new UserLoginInfo(model.Provider, parsedToken.user_id));

            if (!ResultLoginClient.Succeeded)
            {
                //TODO ERROR
                return(BadRequest());
            }
            Service service = null;

            try
            {
                service = await AuthenticationTools.GetServiceActiveAndExists(Request.GetOwinContext().Get <ManahostManagerDAL>(), model.client_id, model.client_secret);
            }
            catch (AuthenticationToolsException e)
            {
                ModelState.AddModelError(e.Key, e.Value);
                return(BadRequest(ModelState));
            }
            var ticket = AuthenticationTools.GenerateTicket(OAuthDefaults.AuthenticationType, model.client_id, client);
            var accessTokenExternal = AuthenticationTools.GenerateToken(ticket, client);

            return(OkWithHeader(accessTokenExternal, new Dictionary <string, string>()
            {
                { GenericNames.OWIN_CONTEXT_CORS_HEADER, service.AllowedOrigin }
            }));
        }