-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.csx
56 lines (49 loc) · 1.82 KB
/
run.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
//Pull out the header values passed into the request
var headers = req.Headers;
if(!headers.TryGetValue("token", out var token))
{
return new BadRequestResult();
}
if(!headers.TryGetValue("client_id", out var client_id))
{
return new BadRequestResult();
}
if(!headers.TryGetValue("client_secret", out var client_secret))
{
return new BadRequestResult();
}
var accessToken = token.First();
var clientId = client_id.First();
var clientSecret = client_secret.First();
//Call the Okta introspection API to validate the token.
var baseUrl = "https://dev-414346.okta.com/oauth2/default/v1/introspect";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("token", accessToken),
new KeyValuePair<string, string>("token_type_hint", "access_token"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret)
});
var _httpClient = new HttpClient();
var response = await _httpClient.PostAsync(baseUrl, content);
var result = await response.Content.ReadAsStringAsync();
log.LogInformation("C# HTTP trigger function processed an external API call to Okta.");
//Based on the token validation from Okta, return a response
if(response.IsSuccessStatusCode)
{
return new OkObjectResult("Hello, you have access to this API");
}
else
{
return new UnauthorizedResult();
}
}