Esempio n. 1
0
        public R <string> GenerateToken(string uid, TimeSpan?timeout = null)
        {
            if (string.IsNullOrEmpty(uid))
            {
                throw new ArgumentNullException(nameof(uid));
            }

            if (!liveTokenList.TryGetValue(uid, out var token))
            {
                token = new ApiToken();
                liveTokenList.Add(uid, token);
            }

            token.Value = TextUtil.GenToken(ApiToken.TokenLen);
            if (timeout.HasValue)
            {
                token.Timeout = timeout.Value == TimeSpan.MaxValue
                                        ? DateTime.MaxValue
                                        : AddTimeSpanSafe(Util.GetNow(), timeout.Value);
            }
            else
            {
                token.Timeout = AddTimeSpanSafe(Util.GetNow(), ApiToken.DefaultTokenTimeout);
            }

            dbTokenList.Upsert(new DbApiToken
            {
                UserUid    = uid,
                Token      = token.Value,
                ValidUntil = token.Timeout
            });

            return(R <string> .OkR(string.Format(TokenFormat, uid, token.Value)));
        }
Esempio n. 2
0
        internal R <ApiToken> GetToken(string uid)
        {
            if (liveTokenList.TryGetValue(uid, out var token) &&
                token.ApiTokenActive)
            {
                return(token);
            }

            var dbToken = dbTokenList.FindById(uid);

            if (dbToken == null)
            {
                return("No active Token");
            }

            if (dbToken.ValidUntil < Util.GetNow())
            {
                dbTokenList.Delete(uid);
                return("No active Token");
            }

            token = new ApiToken {
                Value = dbToken.Token
            };
            liveTokenList[uid] = token;
            return(token);
        }
Esempio n. 3
0
        internal ApiToken?GetToken(string authId)
        {
            if (dbTokenCache.TryGetValue(authId, out var token) &&
                token.ApiTokenActive)
            {
                return(token);
            }

            var dbToken = dbTokenList.FindById(authId);

            if (dbToken is null || dbToken.Token is null)
            {
                return(null);
            }

            if (dbToken.ValidUntil < Tools.Now)
            {
                dbTokenList.Delete(authId);
                dbTokenCache.Remove(authId);
                return(null);
            }

            token = new ApiToken(dbToken.Token, dbToken.ValidUntil);
            dbTokenCache[authId] = token;
            return(token);
        }
Esempio n. 4
0
        internal R <ApiToken, LocalStr> GetToken(string authId)
        {
            if (liveTokenList.TryGetValue(authId, out var token) &&
                token.ApiTokenActive)
            {
                return(token);
            }

            var dbToken = dbTokenList.FindById(authId);

            if (dbToken is null)
            {
                return(new LocalStr(strings.error_no_active_token));
            }

            if (dbToken.ValidUntil < Tools.Now)
            {
                dbTokenList.Delete(authId);
                return(new LocalStr(strings.error_no_active_token));
            }

            token = new ApiToken {
                Value = dbToken.Token
            };
            liveTokenList[authId] = token;
            return(token);
        }
Esempio n. 5
0
 public UserSession(MainBot bot, ClientData client)
 {
     Bot               = bot;
     Client            = client;
     ResponseProcessor = null;
     ResponseData      = null;
     Token             = null;
 }
Esempio n. 6
0
        public R <string> GenerateToken(TimeSpan timeout)
        {
            if (Token == null)
            {
                Token = new ApiToken();
            }

            Token.Value = TextUtil.GenToken(ApiToken.TokenLen);
            var newTimeout = Util.GetNow() + timeout;

            if (newTimeout > Token.Timeout)
            {
                Token.Timeout = newTimeout;
            }

            return(R <string> .OkR(string.Format(TokenFormat, Client.Uid, Token.Value)));
        }
Esempio n. 7
0
        public string GenerateToken(string authId, TimeSpan?timeout = null)
        {
            if (string.IsNullOrEmpty(authId))
            {
                throw new ArgumentNullException(nameof(authId));
            }

            var token = new ApiToken(
                TextUtil.GenToken(ApiToken.TokenLen),
                AddTimeSpanSafe(Tools.Now, timeout ?? ApiToken.DefaultTokenTimeout));

            dbTokenCache[authId] = token;

            dbTokenList.Upsert(new DbApiToken
            {
                UserUid    = authId,
                Token      = token.Value,
                ValidUntil = token.Timeout
            });

            return(string.Format(TokenFormat, authId, token.Value));
        }
Esempio n. 8
0
        public R <string> GenerateToken(string uid, TimeSpan timeout)
        {
            if (string.IsNullOrEmpty(uid))
            {
                throw new ArgumentNullException(nameof(uid));
            }

            if (!tokenList.TryGetValue(uid, out var token))
            {
                token = new ApiToken();
                tokenList.Add(uid, token);
            }

            token.Value = TextUtil.GenToken(ApiToken.TokenLen);
            var newTimeout = Util.GetNow() + timeout;

            if (newTimeout > token.Timeout)
            {
                token.Timeout = newTimeout;
            }

            return(R <string> .OkR(string.Format(TokenFormat, uid, token.Value)));
        }