|
1 | 1 | using Microsoft.AspNetCore.Mvc; |
2 | 2 | using ServicoParticipante.Repository; |
3 | 3 | using ServicoParticipante.Model; |
| 4 | +using Olimpo.Web.Model; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System.Reflection.Metadata.Ecma335; |
4 | 7 |
|
5 | 8 | namespace ServicoParticipante.Controllers; |
6 | 9 | public class ParticipanteController |
7 | 10 | { |
| 11 | + private readonly string _googleApiEndpoint = "https://oauth2.googleapis.com/tokeninfo?id_token="; |
8 | 12 | private static IRepositoryFactory _repositoryFactory = new RepositoryFactory(); |
9 | 13 | private IRepository<Participante> cadastroParticipantes = _repositoryFactory.CreateParticipanteMemoryRepository(); |
10 | 14 | private static int generateId = 0; |
11 | 15 |
|
| 16 | + public Participante? Login(string tokenId) |
| 17 | + { |
| 18 | + var googleResponse = RequestGoogleInformation(tokenId); |
| 19 | + if(googleResponse == null) |
| 20 | + { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + var participante = cadastroParticipantes.FindByPredicate( |
| 25 | + p => p.GoogleId == googleResponse.sub); |
| 26 | + |
| 27 | + if(IsParticipanteValido(participante, googleResponse)) |
| 28 | + { |
| 29 | + return participante; |
| 30 | + } |
| 31 | + |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
12 | 35 | public IEnumerable<Participante> GetParticipanteList() |
13 | 36 | { |
14 | 37 | return cadastroParticipantes.List; |
15 | 38 | } |
| 39 | + |
| 40 | + private GoogleApiResponse? RequestGoogleInformation(string tokenId) |
| 41 | + { |
| 42 | + var participanteGoogleInfo = new GoogleApiResponse(); |
| 43 | + |
| 44 | + HttpClient client = new HttpClient(); |
| 45 | + HttpResponseMessage response = client.GetAsync(_googleApiEndpoint + tokenId).Result; |
| 46 | + if(response.IsSuccessStatusCode) |
| 47 | + { |
| 48 | + string responseBody = response.Content.ReadAsStringAsync().Result; |
| 49 | + participanteGoogleInfo = JsonConvert.DeserializeObject<GoogleApiResponse>(responseBody); |
| 50 | + } |
| 51 | + |
| 52 | + return participanteGoogleInfo; |
| 53 | + } |
| 54 | + |
| 55 | + private bool IsParticipanteValido(Participante participante, GoogleApiResponse googleResponse) |
| 56 | + { |
| 57 | + if(googleResponse == null) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if(participante.GoogleId != googleResponse.sub) |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + if(participante.Email != googleResponse.email) |
| 68 | + { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
16 | 75 |
|
17 | 76 | public Participante? GetParticipanteById(int Id) |
18 | 77 | { |
19 | 78 | var participante = cadastroParticipantes.FindById(Id); |
20 | 79 | return participante; |
21 | 80 | } |
22 | 81 |
|
23 | | - public void CreateParticipante(Participante participante) |
| 82 | + public bool CreateParticipante(Participante participante) |
24 | 83 | { |
25 | | - participante.Id = generateId; |
26 | | - generateId += 1; |
| 84 | + var googleResponse = RequestGoogleInformation(participante.TokenId); |
| 85 | + if (googleResponse == null) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
27 | 89 |
|
28 | | - cadastroParticipantes.Add(participante); |
| 90 | + if (IsParticipanteValido(participante, googleResponse)) |
| 91 | + { |
| 92 | + participante.GoogleId = googleResponse.sub; |
| 93 | + participante.Id = generateId; |
| 94 | + generateId += 1; |
| 95 | + |
| 96 | + cadastroParticipantes.Add(participante); |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
29 | 101 | } |
| 102 | + |
30 | 103 | public bool DeleteParticipanteById(int id) |
31 | 104 | { |
32 | 105 | var participante = cadastroParticipantes.FindById(id); |
|
0 commit comments