-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
236 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Westmoor.DowntimePlanner/ClientApp/src/app/services/business/tenant.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { TenantService } from './tenant.service'; | ||
|
||
describe('TenantService', () => { | ||
let service: TenantService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(TenantService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
Westmoor.DowntimePlanner/ClientApp/src/app/services/business/tenant.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ReplaySubject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TenantService { | ||
readonly current = new ReplaySubject<string | null>(1); | ||
} |
35 changes: 35 additions & 0 deletions
35
...mePlanner/ClientApp/src/app/services/http-interceptors/tenant-http-interceptor.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { TenantService } from '../business/tenant.service'; | ||
import { switchMap, take } from 'rxjs/operators'; | ||
|
||
function whitelist(url: string) { | ||
return url.startsWith('/api'); | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TenantHttpInterceptorService implements HttpInterceptor { | ||
constructor( | ||
private readonly tenant: TenantService | ||
) { | ||
} | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
if (!whitelist(req.url)) { | ||
return next.handle(req); | ||
} | ||
|
||
return this.tenant.current | ||
.pipe( | ||
take(1), | ||
switchMap(tenant => next.handle(tenant | ||
? req.clone({ headers: req.headers.set('X-Tenant-Id', tenant) }) | ||
: req | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace Westmoor.DowntimePlanner.Entities | ||
{ | ||
public class SharedWithEntity | ||
{ | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
public SharedWithKind Kind { get; set; } | ||
public string OwnershipId { get; set; } | ||
public string Picture { get; set; } | ||
public string Username { get; set; } | ||
public string Email { get; set; } | ||
public string Name { get; set; } | ||
public string Email { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Westmoor.DowntimePlanner.Entities | ||
{ | ||
public enum SharedWithKind | ||
{ | ||
User, | ||
Tenant | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
Westmoor.DowntimePlanner/Extensions/AuthorizationExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Westmoor.DowntimePlanner/Security/HttpContextIdentityAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Westmoor.DowntimePlanner.Security | ||
{ | ||
public class HttpContextIdentityAccessor : IIdentityAccessor | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public HttpContextIdentityAccessor(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public string Identity => _httpContextAccessor.HttpContext.User.Identity.Name; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Westmoor.DowntimePlanner/Security/HttpContextTenantAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Westmoor.DowntimePlanner.Security | ||
{ | ||
public class HttpContextTenantAccessor : ITenantAccessor | ||
{ | ||
public class Options | ||
{ | ||
public string AccessibleTenantsClaimType { get; set; } | ||
public string TenantClaimType { get; set; } | ||
} | ||
|
||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly Options _options; | ||
|
||
public HttpContextTenantAccessor(IHttpContextAccessor httpContextAccessor, IOptions<Options> options) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_options = options.Value; | ||
} | ||
|
||
public string[] AccessibleTenants => _httpContextAccessor.HttpContext.User.Claims | ||
.Where(c => c.Type == _options.AccessibleTenantsClaimType) | ||
.Select(c => c.Value) | ||
.ToArray(); | ||
|
||
public string Tenant => _httpContextAccessor.HttpContext.User.Claims | ||
.FirstOrDefault(c => c.Type == _options.TenantClaimType) | ||
?.Value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Westmoor.DowntimePlanner.Security | ||
{ | ||
public interface IIdentityAccessor | ||
{ | ||
string Identity { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Westmoor.DowntimePlanner.Security | ||
{ | ||
public interface ITenantAccessor | ||
{ | ||
string[] AccessibleTenants { get; } | ||
string Tenant { get; } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Westmoor.DowntimePlanner/Security/SecureHeadersIdentityFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
|
||
namespace Westmoor.DowntimePlanner.Security | ||
{ | ||
public static class SecureHeadersIdentityFactory | ||
{ | ||
public static Func<TokenValidatedContext, Task> CreateValidator( | ||
IEnumerable<(string headerKey, string sourceClaimType, string targetClaimType)> mappings | ||
) | ||
{ | ||
return ctx => | ||
{ | ||
var validatedClaims = mappings | ||
.Select(mapping => ( | ||
mapping.sourceClaimType, | ||
mapping.targetClaimType, | ||
headerValue: ctx.Request.Headers | ||
.FirstOrDefault(h => | ||
h.Key.Equals(mapping.headerKey, StringComparison.OrdinalIgnoreCase) | ||
) | ||
.Value | ||
.First() | ||
)) | ||
.Where(mapping => ctx.Principal.Claims | ||
.Any(c => | ||
c.Type == mapping.sourceClaimType && | ||
c.Value == mapping.headerValue | ||
) | ||
) | ||
.Select(mapping => new Claim(mapping.targetClaimType, mapping.headerValue)); | ||
|
||
ctx.Principal.AddIdentity(new ClaimsIdentity(validatedClaims, "SecureHeaders")); | ||
|
||
return Task.CompletedTask; | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.