OOPAndPOP/AuthSample/IApiAuthencator.cs

(完整代码) 1KB

using System;

namespace DesignModeConsole.OOPAndPOP.AuthSample
{
    public interface IApiAuthencator
    {
        /// <summary>
        /// 解析url
        /// </summary>
        /// <param name="url"></param>
        void Auth(string url);

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="apiRequest"></param>
        void Auth(ApiRequest apiRequest);
    }

    public class DefaultApiAuthencatorImpl : IApiAuthencator
    {
        private readonly ICredentialStorage _credentialStorage;

        public DefaultApiAuthencatorImpl()
        {
            _credentialStorage = new MysqlCredentialStorage();
        }

        public DefaultApiAuthencatorImpl(ICredentialStorage credentialStorage)
        {
            _credentialStorage = credentialStorage;
        }

        public void Auth(string url)
        {
            var apiRequest = ApiRequest.CreateFormFullUrl(url);
            Auth(apiRequest);
        }

        public void Auth(ApiRequest apiRequest)
        {
            //拿着解析后的数据开始操作
            AuthToken clientAuthToken = new AuthToken(apiRequest.Token, apiRequest.Timestamp);
            if (clientAuthToken.IdExpired())
                throw new AggregateException("token 过期");

            var password = _credentialStorage.GetPasswordByAppId(apiRequest.AppId);
            var serverAuthToken = AuthToken.Create(apiRequest.BaseUrl, apiRequest.AppId, password, apiRequest.Timestamp);
            if (!serverAuthToken.Match(clientAuthToken))
                throw new AggregateException("token验证失败");
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。