-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Altinn/feature/Add-function-GetPlatformAc…
…cessToken Add function GetPlatformAccessToken
- Loading branch information
Showing
9 changed files
with
155 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Extensions.Http; | ||
using Microsoft.AspNetCore.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using TokenGenerator.Services.Interfaces; | ||
|
||
namespace TokenGenerator | ||
{ | ||
public class GetPlatformAccessToken | ||
{ | ||
private readonly IToken tokenHelper; | ||
private readonly IRequestValidator requestValidator; | ||
private readonly IAuthorization authorization; | ||
private readonly Settings settings; | ||
|
||
public GetPlatformAccessToken(IToken tokenHelper, IRequestValidator requestValidator, IAuthorization authorization, IOptions<Settings> settings) | ||
{ | ||
this.tokenHelper = tokenHelper; | ||
this.requestValidator = requestValidator; | ||
this.authorization = authorization; | ||
this.settings = settings.Value; | ||
} | ||
|
||
[FunctionName(nameof(GetPlatformAccessToken))] | ||
public async Task<ActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) | ||
{ | ||
ActionResult failedAuthorizationResult = await authorization.Authorize(settings.AuthorizedScopePlatform); | ||
if (failedAuthorizationResult != null) | ||
{ | ||
return failedAuthorizationResult; | ||
} | ||
|
||
requestValidator.ValidateQueryParam("env", true, tokenHelper.IsValidEnvironment, out string env); | ||
requestValidator.ValidateQueryParam("app", true, tokenHelper.IsValidDottedIdentifier, out string appClaim); | ||
requestValidator.ValidateQueryParam<uint>("ttl", false, uint.TryParse, out uint ttl, 1800); | ||
|
||
if (requestValidator.GetErrors().Count > 0) | ||
{ | ||
return new BadRequestObjectResult(requestValidator.GetErrors()); | ||
} | ||
|
||
string token = await tokenHelper.GetPlatformAccessToken(env, appClaim, ttl); | ||
|
||
if (!string.IsNullOrEmpty(req.Query["dump"])) | ||
{ | ||
return new OkObjectResult(tokenHelper.Dump(token)); | ||
} | ||
|
||
return new OkObjectResult(token); | ||
} | ||
} | ||
} |
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
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
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