|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.Linq; |
4 | | -using System.Threading; |
5 | 3 | using Microsoft.AspNetCore.Mvc; |
6 | 4 | using Microsoft.Azure.WebJobs; |
7 | 5 | using Microsoft.Azure.WebJobs.Extensions.Http; |
8 | 6 | using Microsoft.AspNetCore.Http; |
9 | 7 | using System.Threading.Tasks; |
10 | 8 | using Microsoft.Extensions.Options; |
11 | 9 | using TokenGenerator.Services.Interfaces; |
| 10 | +using System.Threading; |
| 11 | + |
| 12 | +namespace TokenGenerator; |
12 | 13 |
|
13 | | -namespace TokenGenerator |
| 14 | +public class GetPersonalToken |
14 | 15 | { |
15 | | - public class GetPersonalToken |
| 16 | + private readonly IToken tokenHelper; |
| 17 | + private readonly IRequestValidator requestValidator; |
| 18 | + private readonly IAuthorization authorization; |
| 19 | + private readonly IRandomIdentifier randomIdentifier; |
| 20 | + private readonly IRegisterService registerService; |
| 21 | + private readonly Settings settings; |
| 22 | + |
| 23 | + public GetPersonalToken(IToken tokenHelper, IRequestValidator requestValidator, IAuthorization authorization, IRandomIdentifier randomIdentifier, IRegisterService registerService, IOptions<Settings> settings) |
16 | 24 | { |
17 | | - private readonly IToken tokenHelper; |
18 | | - private readonly IRequestValidator requestValidator; |
19 | | - private readonly IAuthorization authorization; |
20 | | - private readonly IRandomIdentifier randomIdentifier; |
21 | | - private readonly Settings settings; |
| 25 | + this.tokenHelper = tokenHelper; |
| 26 | + this.requestValidator = requestValidator; |
| 27 | + this.authorization = authorization; |
| 28 | + this.randomIdentifier = randomIdentifier; |
| 29 | + this.registerService = registerService; |
| 30 | + this.settings = settings.Value; |
| 31 | + } |
22 | 32 |
|
23 | | - public GetPersonalToken(IToken tokenHelper, IRequestValidator requestValidator, IAuthorization authorization, IRandomIdentifier randomIdentifier, IOptions<Settings> settings) |
| 33 | + [FunctionName(nameof(GetPersonalToken))] |
| 34 | + public async Task<ActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) |
| 35 | + { |
| 36 | + using var cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(req.HttpContext.RequestAborted); |
| 37 | + ActionResult failedAuthorizationResult = await authorization.Authorize(settings.AuthorizedScopePersonal); |
| 38 | + if (failedAuthorizationResult != null) |
24 | 39 | { |
25 | | - this.tokenHelper = tokenHelper; |
26 | | - this.requestValidator = requestValidator; |
27 | | - this.authorization = authorization; |
28 | | - this.randomIdentifier = randomIdentifier; |
29 | | - this.settings = settings.Value; |
| 40 | + return failedAuthorizationResult; |
30 | 41 | } |
31 | 42 |
|
32 | | - [FunctionName(nameof(GetPersonalToken))] |
33 | | - public async Task<ActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) |
| 43 | + requestValidator.ValidateQueryParam("env", true, tokenHelper.IsValidEnvironment, out string env); |
| 44 | + requestValidator.ValidateQueryParam("scopes", false, tokenHelper.TryParseScopes, out string[] scopes, new[] { "altinn:enduser" }); |
| 45 | + requestValidator.ValidateQueryParam("userId", false, uint.TryParse, out uint userId); |
| 46 | + requestValidator.ValidateQueryParam("partyId", false, uint.TryParse, out uint partyId); |
| 47 | + requestValidator.ValidateQueryParam("pid", false, tokenHelper.IsValidPid, out string pid); |
| 48 | + requestValidator.ValidateQueryParam("bulkCount", false, uint.TryParse, out uint bulkCount); |
| 49 | + requestValidator.ValidateQueryParam("authLvl", false, tokenHelper.IsValidAuthLvl, out string authLvl, "3"); |
| 50 | + requestValidator.ValidateQueryParam("consumerOrgNo", false, tokenHelper.IsValidPidOrOrgNo, out string consumerOrgNo, "991825827"); |
| 51 | + requestValidator.ValidateQueryParam("partyuuid", false, Guid.TryParse, out Guid partyUuid); |
| 52 | + requestValidator.ValidateQueryParam("userName", false, tokenHelper.IsValidIdentifier, out string userName, ""); |
| 53 | + requestValidator.ValidateQueryParam("clientAmr", false, tokenHelper.IsValidIdentifier, out string clientAmr, "virksomhetssertifikat"); |
| 54 | + requestValidator.ValidateQueryParam<uint>("ttl", false, uint.TryParse, out uint ttl, 1800); |
| 55 | + requestValidator.ValidateQueryParam("delegationSource", false, tokenHelper.IsValidUri, out string delegationSource); |
| 56 | + requestValidator.ValidateQueryParam("getEnvIds", false, bool.TryParse, out bool getEnvIds); |
| 57 | + |
| 58 | + if (requestValidator.GetErrors().Count > 0) |
34 | 59 | { |
35 | | - ActionResult failedAuthorizationResult = await authorization.Authorize(settings.AuthorizedScopePersonal); |
36 | | - if (failedAuthorizationResult != null) |
37 | | - { |
38 | | - return failedAuthorizationResult; |
39 | | - } |
| 60 | + return new BadRequestObjectResult(requestValidator.GetErrors()); |
| 61 | + } |
40 | 62 |
|
41 | | - requestValidator.ValidateQueryParam("env", true, tokenHelper.IsValidEnvironment, out string env); |
42 | | - requestValidator.ValidateQueryParam("scopes", false, tokenHelper.TryParseScopes, out string[] scopes, new[] { "altinn:enduser" }); |
43 | | - requestValidator.ValidateQueryParam("userId", false, uint.TryParse, out uint userId); |
44 | | - requestValidator.ValidateQueryParam("partyId", false, uint.TryParse, out uint partyId); |
45 | | - requestValidator.ValidateQueryParam("pid", false, tokenHelper.IsValidPid, out string pid); |
46 | | - requestValidator.ValidateQueryParam("bulkCount", false, uint.TryParse, out uint bulkCount); |
47 | | - requestValidator.ValidateQueryParam("authLvl", false, tokenHelper.IsValidAuthLvl, out string authLvl, "3"); |
48 | | - requestValidator.ValidateQueryParam("consumerOrgNo", false, tokenHelper.IsValidPidOrOrgNo, out string consumerOrgNo, "991825827"); |
49 | | - requestValidator.ValidateQueryParam("partyuuid", false, Guid.TryParse, out Guid partyUuid); |
50 | | - requestValidator.ValidateQueryParam("userName", false, tokenHelper.IsValidIdentifier, out string userName, ""); |
51 | | - requestValidator.ValidateQueryParam("clientAmr", false, tokenHelper.IsValidIdentifier, out string clientAmr, "virksomhetssertifikat"); |
52 | | - requestValidator.ValidateQueryParam<uint>("ttl", false, uint.TryParse, out uint ttl, 1800); |
53 | | - requestValidator.ValidateQueryParam("delegationSource", false, tokenHelper.IsValidUri, out string delegationSource); |
54 | | - |
55 | | - if (requestValidator.GetErrors().Count > 0) |
| 63 | + if (bulkCount > 0) |
| 64 | + { |
| 65 | + var randomList = randomIdentifier.GetRandomPersonalIdentifiers(bulkCount); |
| 66 | + var tokenList = await tokenHelper.GetTokenList(randomList, async randomPid => |
| 67 | + await tokenHelper.GetPersonalToken(req, env, scopes, userId, partyId, randomPid, authLvl, consumerOrgNo, userName, clientAmr, ttl, delegationSource, partyUuid)); |
| 68 | + |
| 69 | + return new OkObjectResult(tokenList); |
| 70 | + } |
| 71 | + |
| 72 | + if (getEnvIds) |
| 73 | + { |
| 74 | + if (!settings.EnvPlatformSubscriptionKeyDict.TryGetValue(env, out string subscriptionKey)) |
56 | 75 | { |
57 | | - return new BadRequestObjectResult(requestValidator.GetErrors()); |
| 76 | + return new BadRequestObjectResult($"No subscription key configured for environment: {env}"); |
58 | 77 | } |
59 | 78 |
|
60 | | - if (bulkCount > 0) |
| 79 | + if (string.IsNullOrWhiteSpace(pid)) |
61 | 80 | { |
62 | | - var randomList = randomIdentifier.GetRandomPersonalIdentifiers(bulkCount); |
63 | | - var tokenList = await tokenHelper.GetTokenList(randomList, async randomPid => |
64 | | - await tokenHelper.GetPersonalToken(req, env, scopes, userId, partyId, randomPid, authLvl, consumerOrgNo, userName, clientAmr, ttl, delegationSource, partyUuid)); |
65 | | - |
66 | | - return new OkObjectResult(tokenList); |
| 81 | + return new BadRequestObjectResult("pid is required when getEnvIds is true."); |
67 | 82 | } |
68 | | - |
69 | | - pid ??= randomIdentifier.GetRandomPersonalIdentifiers(1).First(); |
70 | | - string token = await tokenHelper.GetPersonalToken(req, env, scopes, userId, partyId, pid, authLvl, consumerOrgNo, userName, clientAmr, ttl, delegationSource, partyUuid); |
71 | 83 |
|
72 | | - if (!string.IsNullOrEmpty(req.Query["dump"])) |
| 84 | + var platformAccessToken = await tokenHelper.GetPlatformAccessToken(env, settings.PlatformAccessTokenIssuerName, 300); |
| 85 | + var result = await registerService.GetEnvironmentIdentifiers(env, pid, platformAccessToken, subscriptionKey, cancellationSource.Token); |
| 86 | + if (!result.Success) |
73 | 87 | { |
74 | | - return new OkObjectResult(tokenHelper.Dump(token)); |
| 88 | + return new BadRequestObjectResult("Could not retrieve environment identifiers. Check that the pid is valid for the specified environment."); |
75 | 89 | } |
76 | 90 |
|
77 | | - return new OkObjectResult(token); |
| 91 | + userId = result.Party.User.UserId; |
| 92 | + userName = result.Party.User.Username; |
| 93 | + partyId = result.Party.PartyId; |
| 94 | + partyUuid = result.Party.Uuid; |
78 | 95 | } |
| 96 | + |
| 97 | + pid ??= randomIdentifier.GetRandomPersonalIdentifiers(1).First(); |
| 98 | + string token = await tokenHelper.GetPersonalToken(req, env, scopes, userId, partyId, pid, authLvl, consumerOrgNo, userName, clientAmr, ttl, delegationSource, partyUuid); |
| 99 | + |
| 100 | + if (!string.IsNullOrEmpty(req.Query["dump"])) |
| 101 | + { |
| 102 | + return new OkObjectResult(tokenHelper.Dump(token)); |
| 103 | + } |
| 104 | + |
| 105 | + return new OkObjectResult(token); |
79 | 106 | } |
80 | 107 | } |
0 commit comments