-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthRedirect.cs
57 lines (53 loc) · 2.17 KB
/
OAuthRedirect.cs
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
57
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Security.KeyVault.Secrets;
using Azure.Identity;
using System.Collections.Generic;
using System.Net.Http;
namespace PTO
{
public static class OAuthRedirect
{
[FunctionName("OAuthRedirect")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
try
{
var paramList = new Dictionary<string, string>()
{
{"code", req.Query["code"] },
{"client_id", await Secrets.GetClientId()},
{"client_secret", await Secrets.GetClientSecret()}
};
var oauthreq = new HttpRequestMessage(HttpMethod.Post, new Uri(@"https://slack.com/api/oauth.v2.access"))
{
Content = new FormUrlEncodedContent(paramList)
};
var oauthResponse = await Constants.HttpClient.SendAsync(oauthreq);
oauthResponse.EnsureSuccessStatusCode();
string oauthResponseBody = await oauthResponse.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(oauthResponseBody);
string teamId = data?.team?.id;
string accessToken = data?.access_token;
string teamName = data?.team?.name;
//save the code in vault and save the team details in mongo or cosmos
await Secrets.VaultClient.SetSecretAsync(teamId, accessToken);
return new OkObjectResult($"Successfully installed Out-Of-Office Slack app to {teamName} workspace");
}
catch (System.Exception ex)
{
log.LogError($"Error: {ex.StackTrace}");
return new OkObjectResult(string.Empty);
}
}
}
}