Example #1
0
        public static IMAuthCore Instantiate(MAuthVersion version = MAuthVersion.MWS)
        {
            if (version == MAuthVersion.MWSV2)
            {
                return(new MAuthCoreV2());
            }
            else if (version == MAuthVersion.MWS)
            {
                return(new MAuthCore());
            }

            throw new InvalidVersionException($"Version is not recognized:{version}");
        }
        private async Task <bool> Authenticate(HttpRequestMessage request, MAuthVersion version)
        {
            var logMessage = "Mauth-client attempting to authenticate request from app with mauth app uuid" +
                             $" {options.ApplicationUuid} using version {version}";

            logger.LogInformation(logMessage);

            var mAuthCore = MAuthCoreFactory.Instantiate(version);
            var authInfo  = GetAuthenticationInfo(request, mAuthCore);
            var appInfo   = await GetApplicationInfo(authInfo.ApplicationUuid).ConfigureAwait(false);

            var signature = await mAuthCore.GetSignature(request, authInfo).ConfigureAwait(false);

            return(mAuthCore.Verify(authInfo.Payload, signature, appInfo.PublicKey));
        }
        public static HttpRequestMessage ToHttpRequestMessage(this RequestData data, MAuthVersion version = MAuthVersion.MWS)
        {
            var result = new HttpRequestMessage(new HttpMethod(data.Method), data.Url)
            {
                Content = !string.IsNullOrEmpty(data.Base64Content) ?
                          new ByteArrayContent(Convert.FromBase64String(data.Base64Content)) :
                          null,
            };
            var mAuthCore   = MAuthCoreFactory.Instantiate(version);
            var headerKeys  = mAuthCore.GetHeaderKeys();
            var mauthHeader = version == MAuthVersion.MWS
                ? $"{version} {data.ApplicationUuidString}:{data.Payload}"
                : $"{version} {data.ApplicationUuidString}:{data.Payload};";

            result.Headers.Add(headerKeys.mAuthHeaderKey, mauthHeader);
            result.Headers.Add(headerKeys.mAuthTimeHeaderKey, data.SignedTimeUnixSeconds.ToString());

            return(result);
        }