Skip to content

Commit

Permalink
Merge pull request #288 from microsoft/dev
Browse files Browse the repository at this point in the history
reworked adapter authorization +resetdemo
  • Loading branch information
markusheiliger authored Oct 19, 2021
2 parents f4f3871 + 533546c commit 94b58dd
Show file tree
Hide file tree
Showing 95 changed files with 2,098 additions and 1,267 deletions.
2 changes: 0 additions & 2 deletions deploy/bicep/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,4 @@ output configServiceImport object = {
'Endpoint:Orchestrator:AuthCode': orchestrator.outputs.key
'Encryption:KeyStorage': storage_wj.outputs.connectionString
'Audit:ConnectionString': storage_wj.outputs.connectionString
'Adapter:Session:Storage:ConnectoinString': storage_wj.outputs.connectionString
'Adapter:Token:Storage:ConnectionString': storage_wj.outputs.connectionString
}
67 changes: 15 additions & 52 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1069,16 +1069,15 @@
"operationId": "GetDeploymentScope",
"parameters": [
{
"name": "deploymentScopeId",
"name": "organizationId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"nullable": true
"type": "string"
}
},
{
"name": "organizationId",
"name": "deploymentScopeId",
"in": "path",
"required": true,
"schema": {
Expand Down Expand Up @@ -1133,16 +1132,15 @@
"operationId": "UpdateDeploymentScope",
"parameters": [
{
"name": "deploymentScopeId",
"name": "organizationId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"nullable": true
"type": "string"
}
},
{
"name": "organizationId",
"name": "deploymentScopeId",
"in": "path",
"required": true,
"schema": {
Expand Down Expand Up @@ -1206,16 +1204,15 @@
"operationId": "DeleteDeploymentScope",
"parameters": [
{
"name": "deploymentScopeId",
"name": "organizationId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"nullable": true
"type": "string"
}
},
{
"name": "organizationId",
"name": "deploymentScopeId",
"in": "path",
"required": true,
"schema": {
Expand Down Expand Up @@ -1263,13 +1260,13 @@
}
}
},
"/orgs/{organizationId}/scopes/{deploymentScopeId}/authorize": {
"put": {
"/orgs/{organizationId}/scopes/{deploymentScopeId}/authorize/initialize": {
"get": {
"tags": [
"DeploymentScopes"
"DeploymentScopesAuthorization"
],
"summary": "Authorize an existing Deployment Scope.",
"operationId": "AuthorizeDeploymentScope",
"summary": "Initialize a new authorization session for a deployment scope.",
"operationId": "InitializeAuthorization",
"parameters": [
{
"name": "organizationId",
Expand All @@ -1288,33 +1285,9 @@
}
}
],
"requestBody": {
"content": {
"application/json-patch+json": {
"schema": {
"$ref": "#/components/schemas/DeploymentScope"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/DeploymentScope"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/DeploymentScope"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/DeploymentScope"
}
}
}
},
"responses": {
"200": {
"description": "The DeploymentScope was updated.",
"description": "Returns the DeploymentScope that was initialized for an authorization session",
"content": {
"application/json": {
"schema": {
Expand All @@ -1333,16 +1306,6 @@
}
}
},
"404": {
"description": "A DeploymentScope with the id provided was not found.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResult"
}
}
}
},
"401": {
"description": "Unauthorized"
},
Expand Down
47 changes: 12 additions & 35 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,12 @@ paths:
summary: Gets a Deployment Scope.
operationId: GetDeploymentScope
parameters:
- name: deploymentScopeId
- name: organizationId
in: path
required: true
schema:
type: string
nullable: true
- name: organizationId
- name: deploymentScopeId
in: path
required: true
schema:
Expand Down Expand Up @@ -702,13 +701,12 @@ paths:
summary: Updates an existing Deployment Scope.
operationId: UpdateDeploymentScope
parameters:
- name: deploymentScopeId
- name: organizationId
in: path
required: true
schema:
type: string
nullable: true
- name: organizationId
- name: deploymentScopeId
in: path
required: true
schema:
Expand Down Expand Up @@ -747,13 +745,12 @@ paths:
summary: Deletes a Deployment Scope.
operationId: DeleteDeploymentScope
parameters:
- name: deploymentScopeId
- name: organizationId
in: path
required: true
schema:
type: string
nullable: true
- name: organizationId
- name: deploymentScopeId
in: path
required: true
schema:
Expand Down Expand Up @@ -781,12 +778,12 @@ paths:
description: Unauthorized
'403':
description: Forbidden
'/orgs/{organizationId}/scopes/{deploymentScopeId}/authorize':
put:
'/orgs/{organizationId}/scopes/{deploymentScopeId}/authorize/initialize':
get:
tags:
- DeploymentScopes
summary: Authorize an existing Deployment Scope.
operationId: AuthorizeDeploymentScope
- DeploymentScopesAuthorization
summary: Initialize a new authorization session for a deployment scope.
operationId: InitializeAuthorization
parameters:
- name: organizationId
in: path
Expand All @@ -798,23 +795,9 @@ paths:
required: true
schema:
type: string
requestBody:
content:
application/json-patch+json:
schema:
$ref: '#/components/schemas/DeploymentScope'
application/json:
schema:
$ref: '#/components/schemas/DeploymentScope'
text/json:
schema:
$ref: '#/components/schemas/DeploymentScope'
application/*+json:
schema:
$ref: '#/components/schemas/DeploymentScope'
responses:
'200':
description: The DeploymentScope was updated.
description: Returns the DeploymentScope that was initialized for an authorization session
content:
application/json:
schema:
Expand All @@ -825,12 +808,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorResult'
'404':
description: A DeploymentScope with the id provided was not found.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResult'
'401':
description: Unauthorized
'403':
Expand Down
15 changes: 15 additions & 0 deletions src/GlobalExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -10,6 +11,20 @@ namespace TeamCloud
{
internal static class GlobalExtensions
{
internal static string ToHexString(this byte[] bytes)
{
var builder = new StringBuilder(bytes.Length * 2);

foreach (byte b in bytes)
builder.AppendFormat("{0:x2}", b);

return builder.ToString();
}

internal static bool HasCustomAttribute<T>(this MemberInfo memberInfo, bool inherit)
where T: Attribute
=> memberInfo is null ? throw new ArgumentNullException(nameof(memberInfo)) : memberInfo.GetCustomAttributes(typeof(T), inherit).Any();

internal static void Add(this List<Task> tasks, Func<Task> callback)
{
if (tasks is null)
Expand Down
19 changes: 18 additions & 1 deletion src/TeamCloud.API/Auth/AuthExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using TeamCloud.API.Auth.Schemes;
using TeamCloud.API.Services;
using TeamCloud.Data;
using TeamCloud.Model.Data;
Expand Down Expand Up @@ -117,6 +119,21 @@ internal static IServiceCollection AddTeamCloudAuthorization(this IServiceCollec
ProjectUserRole.Admin.AuthPolicy(),
UserRolePolicies.ScheduleWritePolicy);
});

options.AddPolicy(AuthPolicies.AdapterAuthorizationInit, policy =>
{
policy.RequireRole(OrganizationUserRole.Owner.AuthPolicy(),
OrganizationUserRole.Admin.AuthPolicy());
});

options.AddPolicy(AuthPolicies.AdapterAuthorizationFlow, policy =>
{
policy.AddAuthenticationSchemes(AdapterAuthenticationDefaults.AuthenticationScheme);

policy.RequireRole(OrganizationUserRole.Owner.AuthPolicy(),
OrganizationUserRole.Admin.AuthPolicy(),
OrganizationUserRole.Adapter.AuthPolicy());
});
});

return services;
Expand Down Expand Up @@ -145,7 +162,7 @@ internal static async Task<IEnumerable<Claim>> ResolveClaimsAsync(this HttpConte
return claims;

var userRepository = httpContext.RequestServices
.GetRequiredService<IUserRepository>();
.GetRequiredService<TeamCloud.Data.IUserRepository>();

var user = await userRepository
.GetAsync(organizationId, userId)
Expand Down
4 changes: 3 additions & 1 deletion src/TeamCloud.API/Auth/AuthPolicies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static class AuthPolicies
public const string ProjectUserWrite = nameof(ProjectUserWrite);

public const string ProjectComponentOwner = nameof(ProjectComponentOwner);

public const string ProjectScheduleOwner = nameof(ProjectScheduleOwner);

public const string AdapterAuthorizationInit = nameof(AdapterAuthorizationInit);
public const string AdapterAuthorizationFlow = nameof(AdapterAuthorizationFlow);
}
}
17 changes: 17 additions & 0 deletions src/TeamCloud.API/Auth/Schemes/AdapterAuthenticationDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Authentication.Cookies;

namespace TeamCloud.API.Auth.Schemes
{
public static class AdapterAuthenticationDefaults
{
public const string AuthenticationScheme = "Adapter";

public const string AuthenticationType = "Adapter";

public const string QueryParam = "ott";

public const string ClaimType = "ott";

public static readonly string CookiePrefix = CookieAuthenticationDefaults.CookiePrefix;
}
}
24 changes: 24 additions & 0 deletions src/TeamCloud.API/Auth/Schemes/AdapterAuthenticationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
using System.Diagnostics;

namespace TeamCloud.API.Auth.Schemes
{
public static class AdapterAuthenticationExtensions
{
public static AuthenticationBuilder AddAdapterAuthentication(this AuthenticationBuilder authenticationBuilder)
{
authenticationBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>());

return authenticationBuilder.AddScheme<CookieAuthenticationOptions, AdapterAuthenticationHandler>(AdapterAuthenticationDefaults.AuthenticationScheme, options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.SlidingExpiration = true;
});
}
}
}
Loading

0 comments on commit 94b58dd

Please sign in to comment.