Пример #1
0
            private async Task <LoginItem> CreateNewUserLinkedIn(LinkedInUserItem linkUser, CancellationToken token)
            {
                var user = new Student(
                    linkUser.FirstName,
                    linkUser.FirstName + ' ' + linkUser.LastName,
                    linkUser.Email,
                    linkUser.FirstName.ToUpper(),
                    false
                    );

                user.LinkedInId    = linkUser.LinkedInId;
                user.FirstAccess   = true;
                user.EmailVerified = true;

                var randomPassword = Guid.NewGuid().ToString("d").Substring(1, 6);
                var result         = await _userManager.CreateAsync(user, randomPassword);

                var tokenResource = await _tokenGenerator.GenerateUserToken(user);

                await SaveRefreshToken(user.Id, tokenResource.RefreshToken, token);

                return(new LoginItem()
                {
                    TokenInfo = tokenResource,
                    User = new TokenUser()
                    {
                        Name = user.Name,
                        Phone = user.Phone,
                        Cpf = user.Cpf,
                        Email = user.Email
                    }
                });
            }
Пример #2
0
            private async Task <LoginItem> BindUserLinkedIn(User user, LinkedInUserItem linkUser, CancellationToken token)
            {
                user.LinkedInId = linkUser.LinkedInId;

                await _db.UserCollection.ReplaceOneAsync(t =>
                                                         t.Id == user.Id, user,
                                                         cancellationToken : token
                                                         );

                var tokenResource = await _tokenGenerator.GenerateUserToken(user);

                await SaveRefreshToken(user.Id, tokenResource.RefreshToken, token);

                return(new LoginItem()
                {
                    TokenInfo = tokenResource,
                    User = new TokenUser()
                    {
                        Name = user.Name,
                        Phone = user.Phone,
                        Cpf = user.Cpf,
                        Email = user.Email
                    }
                });
            }
Пример #3
0
            private async Task <User> BindUserLinkedIn(User user, LinkedInUserItem linkUser, CancellationToken token)
            {
                user.LinkedInId = linkUser.LinkedInId;

                await _db.UserCollection.ReplaceOneAsync(t =>
                                                         t.Id == user.Id, user,
                                                         cancellationToken : token
                                                         );

                return(user);
            }
Пример #4
0
            public async Task <Result> Handle(Contract request, CancellationToken cancellationToken)
            {
                if (string.IsNullOrEmpty(request.Code))
                {
                    return(Result.Fail("Codigo inválido"));
                }

                if (string.IsNullOrEmpty(request.UserId))
                {
                    return(Result.Fail("Usuário inválido"));
                }

                var userId = ObjectId.Parse(request.UserId);

                var response = await GetAccessToken(request.Code);

                string content = await response.Content.ReadAsStringAsync();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.Fail("Ocorreu um erro ao se comunicar com o LinkedIn"));
                }

                var parsed = JObject.Parse(content);

                var accessToken = (string)parsed["access_token"];

                var responseDados = await GetDadosBasicos(accessToken);

                string contentDados = await responseDados.Content.ReadAsStringAsync();

                var parsedDados = JObject.Parse(contentDados);


                var responseEmail = await GetEmail(accessToken);

                string contentEmail = await responseEmail.Content.ReadAsStringAsync();

                var parsedEmail = JObject.Parse(contentEmail);

                var linkedInUser = new LinkedInUserItem()
                {
                    LinkedInId = (string)parsedDados["id"],
                    FirstName  = (string)parsedDados["firstName"]["localized"]["pt_BR"],
                    LastName   = (string)parsedDados["lastName"]["localized"]["pt_BR"],
                    Email      = (string)parsedEmail["elements"][0]["handle~"]["emailAddress"]
                };

                var linkedUser = await GetUserByLinkedInId(linkedInUser.LinkedInId, cancellationToken);

                if (linkedUser != null)
                {
                    return(Result.Fail("Esta conta do LinkedIn já esta associada a um usuário"));
                }

                var user = await GetUserByUserId(userId, cancellationToken);

                if (user == null)
                {
                    return(Result.Fail("Usuário não existe"));
                }

                return(Result.Ok(await BindUserLinkedIn(user, linkedInUser, cancellationToken)));
            }