Ejemplo n.º 1
0
    public async static Task <string> CreateIdentForTestUser()
    {
        Random       random   = new Random();
        const string chars    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string       username = new string(Enumerable.Repeat(chars, 6).Select(s => s[random.Next(s.Length)]).ToArray());
        string       email    = String.Format("user{0}@prvd.local", username);

        await Ident.CreateUser(new User()
        {
            FirstName = "Test",
            LastName  = "User",
            Email     = email,
            Password  = "******",
        });

        return((await Ident.Authenticate(
                    new Auth
        {
            Email = email,
            Password = "******",
        }
                    )).Token.Token);
    }
Ejemplo n.º 2
0
        public async Task <(bool, byte[])> AuthenticateOrgAsync(string email, string password)
        {
            bool isAuthenticated = false;

            byte[] token = null;

            try
            {
                _logger.LogTrace("calling Ident Authenticate");
                var userAuthResponse = await Ident.Authenticate(email, password);

                if (userAuthResponse.Item1 == 201)
                {
                    _logger.LogInformation(String.Format("Ident Authentication succesful for email {0}", email), userAuthResponse);

                    var tkn_dict = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, object> > >(userAuthResponse.Item2);
                    if (tkn_dict.ContainsKey("token"))
                    {
                        var tkn_dict_inner = tkn_dict["token"];
                        if (tkn_dict_inner.ContainsKey("token"))
                        {
                            var tkn_str = tkn_dict_inner["token"];

                            _logger.LogTrace("calling InitIdent");
                            this._ident = Ident.InitIdent(tkn_str.ToString());

                            var orgId = await this.fetchDefaultOrganizationIdAsync();

                            if (orgId != null)
                            {
                                token = await this.vendOrganizationJWTAsync(orgId);
                            }

                            isAuthenticated = token != null;

                            if (isAuthenticated)
                            {
                                _logger.LogInformation(String.Format("Token Vending Authentication succesful for email {0}", email));

                                _repository.UpdateParticipants(email, Encoding.UTF8.GetString(token));
                            }
                        }
                        else
                        {
                            _logger.LogError("Inner dictionary of Ident response does not contain 'token' key");
                        }
                    }
                    else
                    {
                        _logger.LogError("Ident response does not contain 'token' key");
                    }
                }
                else
                {
                    _logger.LogError("Ident Authentication did not work: {0}, {1}", userAuthResponse.Item1, userAuthResponse.Item2);
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Unexpected Exception in Authenticate");
            }

            return(isAuthenticated, token);
        }