Exemplo n.º 1
0
        /// <summary>
        /// Gets different type of tokens by Code using Oxd Server
        /// </summary>
        /// <param name="host">Oxd Host</param>
        /// <param name="port">Oxd Port</param>
        /// <param name="getTokensByCodeParams">Input params for Get Tokens by Code command</param>
        /// <returns></returns>
        public GetTokensByCodeResponse GetTokensByCode(string host, int port, GetTokensByCodeParams getTokensByCodeParams)
        {
            Logger.Info("Verifying input parameters.");
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("Oxd Host should not be NULL.");
            }

            if (port <= 0)
            {
                throw new ArgumentNullException("Oxd Port should be a valid port number.");
            }

            if (getTokensByCodeParams == null)
            {
                throw new ArgumentNullException("The get tokens by code command params should not be NULL.");
            }

            if (string.IsNullOrEmpty(getTokensByCodeParams.OxdId))
            {
                throw new MissingFieldException("Oxd ID is required for getting tokens.");
            }

            if (string.IsNullOrEmpty(getTokensByCodeParams.Code))
            {
                throw new MissingFieldException("Auth Code is required for getting tokens.");
            }

            if (string.IsNullOrEmpty(getTokensByCodeParams.State))
            {
                throw new MissingFieldException("Auth State is required for getting tokens.");
            }

            try
            {
                Logger.Info("Preparing and sending command.");
                var cmdGetTokensByCode = new Command {
                    CommandType = CommandType.get_tokens_by_code, CommandParams = getTokensByCodeParams
                };
                var    commandClient   = new CommandClient(host, port);
                string commandResponse = commandClient.send(cmdGetTokensByCode);

                var response = JsonConvert.DeserializeObject <GetTokensByCodeResponse>(commandResponse);
                Logger.Info(string.Format("Got response status as {0} and access token is {1}", response.Status, response.Data.AccessToken));

                return(response);
            }
            catch (Exception ex)
            {
                Logger.Log(NLog.LogLevel.Error, ex, "Exception when getting tokens of site.");
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets different type of tokens by Code using http
        /// </summary>
        /// <param name="oxdWebUrl">Oxd to http REST service URL</param>
        /// <param name="getTokensByCodeParams">Input params for Get Tokens by Code command</param>
        /// <returns></returns>

        public GetTokensByCodeResponse GetTokensByCode(string oxdWebUrl, GetTokensByCodeParams getTokensByCodeParams)
        {
            Logger.Info("Verifying input parameters.");
            if (string.IsNullOrEmpty(oxdWebUrl))
            {
                throw new ArgumentNullException("Oxd Rest Service URL should not be NULL.");
            }

            try
            {
                var cmdGetTokensByCode = new Command {
                    CommandType = RestCommandType.get_tokens_by_code, CommandParams = getTokensByCodeParams
                };
                var    commandClient   = new CommandClient(oxdWebUrl);
                string commandResponse = commandClient.send(cmdGetTokensByCode);
                var    response        = JsonConvert.DeserializeObject <GetTokensByCodeResponse>(commandResponse);
                return(response);
            }
            catch (Exception ex)
            {
                Logger.Log(NLog.LogLevel.Error, ex, "Exception when getting Token Code.");
                return(null);
            }
        }
Exemplo n.º 3
0
        public ActionResult GetUserInfo()
        {
            string code  = Request.Params["code"];
            string state = Request.Params["state"];

            if (code == null && state == null)
            {
                return(RedirectToAction("Index"));
            }

            OxdModel oxd = new OxdModel();

            #region GetProtectionAccessToken
            string protectionAccessToken = "";

            if (dynamic_registration)
            {
                //For OXD Server
                if (OXDType == "local")
                {
                    //Get ProtectionAccessToken
                    protectionAccessToken = GetProtectionAccessToken(oxdHost, oxdPort);
                }

                //For OXD Web
                if (OXDType == "web")
                {
                    protectionAccessToken = GetProtectionAccessToken(httpresturl);
                }
            }
            #endregion


            #region Get Tokens by Code

            var getTokenByCodeInputParams = new GetTokensByCodeParams();
            var getTokenByCodeClient      = new GetTokensByCodeClient();

            //prepare input params for Getting Tokens from a site
            getTokenByCodeInputParams.OxdId = oxd_id;
            getTokenByCodeInputParams.Code  = code;
            getTokenByCodeInputParams.State = state;
            getTokenByCodeInputParams.ProtectionAccessToken = protectionAccessToken;

            var getTokensByCodeResponse = new GetTokensByCodeResponse();

            //For OXD Server
            if (OXDType == "local")
            {
                getTokensByCodeResponse = getTokenByCodeClient.GetTokensByCode(oxdHost, oxdPort, getTokenByCodeInputParams);
            }

            //For OXD Web
            if (OXDType == "web")
            {
                getTokensByCodeResponse = getTokenByCodeClient.GetTokensByCode(httpresturl, getTokenByCodeInputParams);
            }


            var accessToken  = getTokensByCodeResponse.Data.AccessToken;
            var refreshToken = getTokensByCodeResponse.Data.RefreshToken;
            #endregion


            #region Get Access Token By Refresh Token
            if (dynamic_registration)
            {
                var getAccessTokenByRefreshTokenInputParams = new GetAccessTokenByRefreshTokenParams();
                var getAccessTokenByRefreshTokenClient      = new GetAccessTokenByRefreshTokenClient();

                //prepare input params for Getting Tokens from a site
                getAccessTokenByRefreshTokenInputParams.OxdId                 = oxd_id;
                getAccessTokenByRefreshTokenInputParams.RefreshToken          = refreshToken;
                getAccessTokenByRefreshTokenInputParams.ProtectionAccessToken = protectionAccessToken;


                var getAccessTokenByRefreshTokenResponse = new GetAccessTokenByRefreshTokenResponse();

                //For OXD Server
                if (OXDType == "local")
                {
                    getAccessTokenByRefreshTokenResponse = getAccessTokenByRefreshTokenClient.GetAccessTokenByRefreshToken(oxdHost, oxdPort, getAccessTokenByRefreshTokenInputParams);
                }

                //For OXD Web
                if (OXDType == "web")
                {
                    getAccessTokenByRefreshTokenResponse = getAccessTokenByRefreshTokenClient.GetAccessTokenByRefreshToken(httpresturl, getAccessTokenByRefreshTokenInputParams);
                }


                accessToken  = getAccessTokenByRefreshTokenResponse.Data.AccessToken;
                refreshToken = getAccessTokenByRefreshTokenResponse.Data.RefreshToken;
            }
            #endregion


            #region Get User Info

            var getUserInfoInputParams = new GetUserInfoParams();
            var getUserInfoClient      = new GetUserInfoClient();

            //prepare input params for Getting User Info from a site
            getUserInfoInputParams.OxdId                 = oxd_id;
            getUserInfoInputParams.AccessToken           = accessToken;
            getUserInfoInputParams.ProtectionAccessToken = protectionAccessToken;


            var getUserInfoResponse = new GetUserInfoResponse();

            //For OXD Server
            if (OXDType == "local")
            {
                getUserInfoResponse = getUserInfoClient.GetUserInfo(oxdHost, oxdPort, getUserInfoInputParams);
            }

            //For OXD Web
            if (OXDType == "web")
            {
                getUserInfoResponse = getUserInfoClient.GetUserInfo(httpresturl, getUserInfoInputParams);
            }


            //Process response


            Session["userName"]  = getUserInfoResponse.Data.UserClaims["name"].FirstOrDefault();
            Session["userEmail"] = getUserInfoResponse.Data.UserClaims["email"] == null ? string.Empty : getUserInfoResponse.Data.UserClaims["email"].FirstOrDefault();
            #endregion

            return(RedirectToAction("UserInfo"));
        }