예제 #1
0
        public IEnumerable <Entities.Token> GetTokensByUser(string _email)
        {
            ISession localSession = GetSession();
            IMapper  mapper       = new Mapper(localSession);

            var CQLstr = localSession.Prepare(@"SELECT tokendata, creationdate, email, expirationdate, devicename, blacklisted
                              FROM tokens 
                              WHERE email = ?");

            List <Entities.Token> result = new List <Entities.Token>();

            var rs = localSession.Execute(CQLstr.Bind(new { email = _email }));

            foreach (var row in rs)
            {
                Entities.Token t = new Entities.Token()
                {
                    Email          = _email,
                    DeviceName     = row.GetValue <string>("devicename"),
                    CreationDate   = row.GetValue <DateTime>("creationdate"),
                    ExpirationDate = row.GetValue <DateTime>("expirationdate"),
                    Blacklisted    = row.GetValue <Boolean>("blacklisted"),
                    TokenData      = row.GetValue <string>("tokendata")
                };
                result.Add(t);
            }

            return(result);
        }
예제 #2
0
 public AuthReturnState SaveToken(Token token, UserInfo user, AppData app)
 {
     if (UserSession != null)
     {
         UserSession.TokenLocal = token;
         return AuthReturnState.Success;
     }
     else
         return AuthReturnState.SessionIsNull;
 }
예제 #3
0
        public TokenResult AddToken(Token token)
        {
            try
            {
                _context.Tokens.Add(token);
                _context.SaveChanges();
                return TokenResult.Success;
            }
            catch (Exception)
            {
                return TokenResult.InternalError;
            }        

        }
예제 #4
0
        public void SaveToken(Entities.Token _token)
        {
            ISession localSession = GetSession();

            var CQLstr = localSession.Prepare(@"INSERT INTO tokens (tokendata, creationdate, email, expirationdate, devicename, blacklisted) 
                                                VALUES (:tokendata, :crtDate, :email, :expDate, :device, :blacklisted)");

            //cassandra doesnt want to change timezone from gmt - hardcoded solution for now
            //_token.DeviceName = "desktop"; //mora biti definiran jer je sada primary
            localSession.Execute(CQLstr.Bind(new
            {
                tokendata   = _token.TokenData,
                crtDate     = _token.CreationDate.AddHours(2),
                email       = _token.Email,
                expDate     = _token.ExpirationDate.AddHours(2),
                device      = _token.DeviceName,
                blacklisted = false
            }));
        }
예제 #5
0
        public IHttpActionResult GetToken(string code, string appId, string appSecretKey, string responseType, string grantType)
        {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            if (string.IsNullOrEmpty(appId))
                throw new ArgumentNullException("appId");

            if (string.IsNullOrEmpty(appSecretKey))
                throw new ArgumentNullException("appSecretKey");

            if (string.IsNullOrEmpty(responseType))
                throw new ArgumentNullException("responseType");

            if (responseType != "token")
                throw new ArgumentException("Неправильное значение responseType");

            if (string.IsNullOrEmpty(grantType))
                throw new ArgumentNullException("grantType");

            if (grantType != "code")
                throw new ArgumentException("Неправильное значение grantType");

            var app = _appDataReposytory.AppDatas.FirstOrDefault(e => e.Id.ToString() == appId);
            if (app == null)
                throw new Exception("Нет приложения с таким appId");

            if (app.AppSecretId != appSecretKey)
                throw new ArgumentException("Неверное значение appSecretKey");

            var authCode = _codes.FirstOrDefault(e => e.Key == code);
            if (authCode.Key == null)
                throw new ArgumentException("Неверное значение code");

            var user = _userReposytory.UserInfoes.FirstOrDefault(e => e.Id == authCode.Value);
            _codes.Remove(authCode.Key);

            var token = new Token
            {
                UserId = user.Id,
                AccessToken = user.Id + Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                RefreshToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                ExpiresIn = DateTime.UtcNow.AddHours(2).ToString(CultureInfo.InvariantCulture),
                TokenType = "Bearer"
            };

            _tokenRepository.AddToken(token);
            return Ok(token);
        }