Example #1
0
        public AuthorizeValueModel SendRequestAndAuthorizeByGet(string protectedServerUrl, AuthorizeValueModel authorizeModel)
        {
            //Hash(r)^(n-i)
            int    minusValue  = authorizeModel.AuthZTimes - authorizeModel.CurrentTimes;
            string hashNMinusI = HashMultipleTimes(authorizeModel.RandomValue, minusValue);

            //初始化請求授權
            string hashNMinusIAddOne = MD5Hasher.Hash(hashNMinusI);

            string authZKey = GetResrcClientKeyAuthzTimesValue(authorizeModel.ClientProtectedCryptoModel.Key, authorizeModel.ClientTempId, authorizeModel.CurrentTimes);
            string authZIv  = GetResrcClientKeyAuthzTimesValue(authorizeModel.ClientProtectedCryptoModel.IV, authorizeModel.ClientTempId, authorizeModel.CurrentTimes);

            string currentTimesCypherText = GetCurrentTimesCypherText(authorizeModel, hashNMinusI, authZKey, authZIv);

            string token = GetTokenByAuthorizeDataAndCurrentTimesCypherText(authorizeModel, currentTimesCypherText);

            Dictionary <string, string> headers = new Dictionary <string, string>
            {
                { "ClientId", clientResource.ClientId },
                { "Token", token }
            };
            // 向資源保護者請求授權
            ApiResult <string> rescrAuthorizeRespOpt = AuthenHttpHandler.SendRequestByGet <string>(protectedServerUrl, headers);

            TimesCypherTextPrimeModel timesCypherTextPrimeModel = DecryptProtectedServerResult(authZKey, authZIv, rescrAuthorizeRespOpt);

            bool checkAuthZValueResult = CheckProtectedServerRespAuthZValue(timesCypherTextPrimeModel);

            if (checkAuthZValueResult == false)
            {
                throw new Exception("CheckProtectedServerRespAuthZValue is fail.");
            }
            authorizeModel.CurrentTimes           = authorizeModel.CurrentTimes + 1;
            authorizeModel.ClientTempId.HashValue = hashNMinusI;

            return(authorizeModel);
        }