Example #1
0
        public async Task <string> GenerateStateAsync(string remoteAddress, ActionToken serverToken, CancellationToken cancellationToken = default)
        {
            var state    = Guid.NewGuid().ToString();
            var tkstring = serverToken.ExportString();

            await this.Redis.CreateTemporaryValueAsync(remoteAddress, TimeSpan.FromMinutes(5), OAuthKey, state);

            await this.Redis.CreateTemporaryValueAsync(tkstring, TimeSpan.FromMinutes(5), OAuthKey, state, OAuthTokenKey);

            return(OAuthPrefix + state);
        }
Example #2
0
        /// <summary>
        /// Issue a new token pair with specified state.
        /// </summary>
        /// <param name="actionId">Action for which the token pair is issued.</param>
        /// <param name="state">State for the token.</param>
        /// <returns>Issued token pair or null if issuing fails.</returns>
        public ActionTokenPair IssueTokenPair(string actionId, byte[] state)
        {
            byte[] kclient, kserver, sigclient = new byte[SignatureSize], sigserver = new byte[SignatureSize];
            using (var rsa = RSA.Create(RsaSize))
            {
                kclient = rsa.ExportRSAPublicKey();
                kserver = rsa.ExportRSAPrivateKey();
            }

            if (!this.GenerateSignatures(actionId, state, kclient, kserver, sigclient, sigserver))
            {
                return(null);
            }

            var tkclient = new ActionToken(true, kclient, state, sigclient);
            var tkserver = new ActionToken(false, kserver, state, sigserver);

            return(new ActionTokenPair(tkclient, tkserver));
        }
Example #3
0
        public async Task <ActionToken> ValidateStateAsync(string remoteAddress, string state, CancellationToken cancellationToken = default)
        {
            if (!state.AsSpan().StartsWith(OAuthPrefix))
            {
                return(null);
            }

            var statestr = new string(state.AsSpan(OAuthPrefix.Length));

            var refAddr = await this.Redis.GetValueAsync <string>(OAuthKey, statestr);

            var srcTokn = await this.Redis.GetValueAsync <string>(OAuthKey, statestr, OAuthTokenKey);

            await this.Redis.DeleteValueAsync(OAuthKey, statestr);

            await this.Redis.DeleteValueAsync(OAuthKey, statestr, OAuthTokenKey);

            if (refAddr != remoteAddress || !ActionToken.TryParse(srcTokn, out var actionToken))
            {
                return(null);
            }

            return(actionToken);
        }
Example #4
0
 static void Base64(Span <char> buff, ActionToken state)
 => state.TryExportString(buff, out _);