Skip to content

Commit a5f5298

Browse files
committed
login
1 parent 220b88b commit a5f5298

13 files changed

Lines changed: 152 additions & 47 deletions

File tree

5.5 KB
Binary file not shown.

SOA-Backend/Gateway-api/appsettings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
},
5555
{
5656
"UseServiceDiscovery": true,
57-
"DownstreamPathTemplate": "/api/login",
57+
"DownstreamPathTemplate": "/api/login/{tokenId}",
5858
"DownstreamScheme": "http",
5959
"ServiceName": "ServicoParticipante",
60-
"UpstreamPathTemplate": "/api/login",
61-
"UpstreamHttpMethod": ["Post"],
60+
"UpstreamPathTemplate": "/api/login/{tokenId}",
61+
"UpstreamHttpMethod": ["Get"],
6262
"ReRoutesCaseSensitive": false
6363
},
6464
{

SOA-Backend/Gateway-api/ocelot.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
},
4848
{
4949
"UseServiceDiscovery": true,
50-
"DownstreamPathTemplate": "/api/login",
50+
"DownstreamPathTemplate": "/api/login/{tokenId}",
5151
"DownstreamScheme": "http",
5252
"ServiceName": "ServicoParticipante",
53-
"UpstreamPathTemplate": "/api/login",
54-
"UpstreamHttpMethod": ["Post"],
53+
"UpstreamPathTemplate": "/api/login/{tokenId}",
54+
"UpstreamHttpMethod": ["Get"],
5555
"ReRoutesCaseSensitive": false
5656
},
5757
{

SOA-Backend/ServicoEquipe/Model/Participante.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public partial class Participante : EntidadeBase
99
[Required]
1010
public string TokenId { get; set; }
1111

12+
[Required]
13+
public string GoogleId { get; set; }
14+
1215
[Required]
1316
public string Name { get; set; }
1417

SOA-Backend/ServicoEvento/Model/Participante.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public partial class Participante : EntidadeBase
99
[Required]
1010
public string TokenId { get; set; }
1111

12+
[Required]
13+
public string GoogleId { get; set; }
14+
1215
[Required]
1316
public string Name { get; set; }
1417

@@ -20,4 +23,4 @@ public partial class Participante : EntidadeBase
2023

2124
[Required]
2225
public DateTime BirthDay { get; set; }
23-
}
26+
}

SOA-Backend/ServicoParticipante/Controllers/ParticipanteController.cs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,105 @@
11
using Microsoft.AspNetCore.Mvc;
22
using ServicoParticipante.Repository;
33
using ServicoParticipante.Model;
4+
using Olimpo.Web.Model;
5+
using Newtonsoft.Json;
6+
using System.Reflection.Metadata.Ecma335;
47

58
namespace ServicoParticipante.Controllers;
69
public class ParticipanteController
710
{
11+
private readonly string _googleApiEndpoint = "https://oauth2.googleapis.com/tokeninfo?id_token=";
812
private static IRepositoryFactory _repositoryFactory = new RepositoryFactory();
913
private IRepository<Participante> cadastroParticipantes = _repositoryFactory.CreateParticipanteMemoryRepository();
1014
private static int generateId = 0;
1115

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+
1235
public IEnumerable<Participante> GetParticipanteList()
1336
{
1437
return cadastroParticipantes.List;
1538
}
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+
1675

1776
public Participante? GetParticipanteById(int Id)
1877
{
1978
var participante = cadastroParticipantes.FindById(Id);
2079
return participante;
2180
}
2281

23-
public void CreateParticipante(Participante participante)
82+
public bool CreateParticipante(Participante participante)
2483
{
25-
participante.Id = generateId;
26-
generateId += 1;
84+
var googleResponse = RequestGoogleInformation(participante.TokenId);
85+
if (googleResponse == null)
86+
{
87+
return false;
88+
}
2789

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;
29101
}
102+
30103
public bool DeleteParticipanteById(int id)
31104
{
32105
var participante = cadastroParticipantes.FindById(id);

SOA-Backend/ServicoParticipante/Model/Participante.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
namespace ServicoParticipante.Model;
55

6-
[Table("Participante")]
7-
public partial class Participante : EntidadeBase
6+
[Table("Participante")]public partial class Participante : EntidadeBase
87
{
98
[Required]
109
public string TokenId { get; set; }
1110

11+
[Required]
12+
public string GoogleId { get; set; }
13+
1214
[Required]
1315
public string Name { get; set; }
1416

SOA-Backend/ServicoParticipante/Web/Api.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Mvc;
22
using ServicoParticipante.Controllers;
33
using ServicoParticipante.Model;
4-
using ServicoParticipante.Web.Model;
4+
using Olimpo.Web.Model;
55

66
namespace ServicoParticipante.Web
77
{
@@ -10,11 +10,17 @@ namespace ServicoParticipante.Web
1010
public class Api : ControllerBase
1111
{
1212
private static Fachada _fachada = new Fachada();
13-
[HttpPost]
14-
[Route("api/login")]
15-
public IActionResult LoginParticipante([FromBody] LoginRequest loginRequest)
13+
[HttpGet]
14+
[Route("api/login/{tokenId}")]
15+
public IActionResult LoginParticipante(string tokenId)
1616
{
17-
return Ok();
17+
var participante = _fachada.logarParticipante(tokenId);
18+
if(participante == null)
19+
{
20+
return BadRequest();
21+
}
22+
23+
return Ok(participante);
1824
}
1925

2026
[HttpGet]
@@ -51,16 +57,20 @@ public IActionResult GetParticipanteById(int Id)
5157

5258
[HttpPost]
5359
[Route("api/participante")]
54-
public IActionResult CreateParticipante([FromBody] Participante participante)
60+
public IActionResult CreateParticipante([FromBody] Participante request)
5561
{
56-
if(participante == null)
62+
if (request == null)
5763
{
5864
return BadRequest("Invalid data.");
5965
}
6066

61-
_fachada.cadastrarParticipante(participante);
67+
var isCreated = _fachada.cadastrarParticipante(request);
68+
if (isCreated)
69+
{
70+
return StatusCode(StatusCodes.Status201Created, request);
71+
}
6272

63-
return StatusCode(StatusCodes.Status201Created, participante);
73+
return NoContent();
6474
}
6575

6676
[HttpDelete]

SOA-Backend/ServicoParticipante/Web/Fachada.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ class Fachada
88
{
99
private ParticipanteController participanteController = new ParticipanteController();
1010

11-
public void cadastrarParticipante(Participante participante)
11+
public Participante? logarParticipante(string tokenId)
1212
{
13-
participanteController.CreateParticipante(participante);
13+
return participanteController.Login(tokenId);
14+
}
15+
16+
public bool cadastrarParticipante(Participante participante)
17+
{
18+
return participanteController.CreateParticipante(participante);
1419
}
1520

1621
public Participante buscarParticipante(int Id)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
namespace ServicoParticipante.Web.Model
1+
namespace Olimpo.Web.Model
22
{
33
public class LoginRequest
44
{
5-
//todo(felipe.almeida): implementar quando decidir campos a serem utilizados
5+
public string tokenId { get; set; }
66
}
77
}

0 commit comments

Comments
 (0)