| @@ -47,9 +47,9 @@ namespace IdentityProvider.Services | |||
| //AUTHORIZATION HEADER | |||
| http.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Basic " + Convert.ToBase64String(contentType)); | |||
| //ACCEPT HEADER | |||
| http.DefaultRequestHeaders.Accept.Add( | |||
| new MediaTypeWithQualityHeaderValue("application/json")); | |||
| ////ACCEPT HEADER | |||
| //http.DefaultRequestHeaders.Accept.Add( | |||
| // new MediaTypeWithQualityHeaderValue("application/json")); | |||
| //BODY PARAMS | |||
| var requestBody = new Dictionary<string, string>(); | |||
| @@ -61,12 +61,14 @@ namespace IdentityProvider.Services | |||
| var response = await http.PostAsync(url, new FormUrlEncodedContent(requestBody)); | |||
| var contents = JsonConvert.DeserializeObject<TokenResponse>(await response.Content.ReadAsStringAsync()); | |||
| return new TokenResponse | |||
| { | |||
| access_token = contents!.access_token, | |||
| refresh_token = contents!.refresh_token, | |||
| expires_in = contents!.expires_in | |||
| }; | |||
| if (contents != null) | |||
| return new TokenResponse | |||
| { | |||
| access_token = contents!.access_token, | |||
| refresh_token = contents!.refresh_token, | |||
| expires_in = contents!.expires_in | |||
| }; | |||
| else return null; | |||
| } | |||
| public async Task<CodeRequest> GetAuthParams() | |||
| @@ -1,4 +1,5 @@ | |||
| <CascadingAuthenticationState> | |||
| @using NemAnBlazor.Pages | |||
| <CascadingAuthenticationState> | |||
| <Router AppAssembly="@typeof(App).Assembly"> | |||
| <Found Context="routeData"> | |||
| <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" > | |||
| @@ -1,26 +0,0 @@ | |||
| using Blazored.SessionStorage; | |||
| using System.Security.Claims; | |||
| namespace NemAnBlazor | |||
| { | |||
| public class AuthProvider : AuthenticationStateProvider | |||
| { | |||
| private readonly ISessionStorageService _sessionStorage; | |||
| public AuthProvider(ISessionStorageService sessionStorage) | |||
| { | |||
| _sessionStorage = sessionStorage; | |||
| } | |||
| public override async Task<AuthenticationState> GetAuthenticationStateAsync() | |||
| { | |||
| string token = await _sessionStorage.GetItemAsync<string>("token"); | |||
| ClaimsIdentity identity = new (); | |||
| ClaimsPrincipal user = new (identity); | |||
| AuthenticationState state = new(user); | |||
| NotifyAuthenticationStateChanged(Task.FromResult(state)); | |||
| return state; | |||
| } | |||
| } | |||
| } | |||
| @@ -6,6 +6,14 @@ | |||
| <ImplicitUsings>enable</ImplicitUsings> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | |||
| <Optimize>True</Optimize> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | |||
| <Optimize>False</Optimize> | |||
| </PropertyGroup> | |||
| <ItemGroup> | |||
| <PackageReference Include="Blazored.SessionStorage" Version="2.2.0" /> | |||
| <PackageReference Include="Grpc.Net.Client" Version="2.47.0" /> | |||
| @@ -10,8 +10,8 @@ | |||
| @code { | |||
| protected override async Task OnInitializedAsync() | |||
| { | |||
| string url = NavigationMgr.Uri; | |||
| @@ -21,11 +21,12 @@ | |||
| var response = await AuthService.GetAccessToken(new GrpcShared.DTO.Auth.TokenRequest { code = code}); | |||
| //if (response.access_token == null) NavigationMgr.NavigateTo("/"); | |||
| //store access token in local storage | |||
| await sessionStorage.SetItemAsync("token", response.access_token); | |||
| await sessionStorage.SetItemAsync("refresh_token", response.refresh_token); | |||
| //redirect to home | |||
| NavigationMgr.NavigateTo("/home"); | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| @page "/home" | |||
| @attribute [Authorize] | |||
| <h3>Home</h3> | |||
| <p>login radi</p> | |||
| @@ -7,7 +8,7 @@ | |||
| <p>autorizovan si</p> | |||
| </Authorized> | |||
| <NotAuthorized> | |||
| <p>nisi autorizovan</p> | |||
| <LoginRedirect/> | |||
| </NotAuthorized> | |||
| </AuthorizeView> | |||
| @code { | |||
| @@ -0,0 +1,7 @@ | |||
| @inject NavigationManager UriHelper | |||
| @code { | |||
| protected override void OnInitialized() | |||
| { | |||
| UriHelper.NavigateTo("/"); | |||
| } | |||
| } | |||
| @@ -29,7 +29,8 @@ builder.Services.AddScoped<AuthenticationStateProvider, AuthClientService>(); | |||
| builder.Services.AddScoped<ITrackClientService, TrackClientService>(); | |||
| builder.Services.AddScoped<IAuthClientService, AuthClientService>(); | |||
| builder.Services.AddBlazoredSessionStorage(); | |||
| builder.Services.AddScoped<AuthenticationStateProvider, AuthProvider>(); | |||
| builder.Services.AddScoped<AuthenticationStateProvider, AuthClientService>(); | |||
| builder.Services.AddAuthorizationCore(); | |||
| await builder.Build().RunAsync(); | |||
| @@ -27,6 +27,31 @@ namespace NemAnBlazor.Services | |||
| return await _serviceClient.GetAccessToken(request); | |||
| } | |||
| public override async Task<AuthenticationState> GetAuthenticationStateAsync() | |||
| { | |||
| string token = await _sessionStorage.GetItemAsync<string>("token"); | |||
| //token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN"; | |||
| if (token == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); | |||
| var userInfo = await _serviceClient.GetUserInfo(new TokenMessage { Token = token }); | |||
| List<Claim> claims = new(); | |||
| claims.Add(new Claim("email", userInfo.email!)); | |||
| claims.Add(new Claim("id", userInfo.id!)); | |||
| claims.Add(new Claim("name", userInfo.display_name!)); | |||
| ClaimsIdentity identity = new(claims, "jwt"); | |||
| //ClaimsIdentity identity = new(); | |||
| ClaimsPrincipal user = new(identity); | |||
| AuthenticationState state = new(user); | |||
| NotifyAuthenticationStateChanged(Task.FromResult(state)); | |||
| return state; | |||
| } | |||
| public async Task<CodeRequest> GetAuthParams() | |||
| { | |||
| return await _serviceClient.GetAuthParams(); | |||