-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolicySourceCode.cs
More file actions
105 lines (95 loc) · 4.37 KB
/
PolicySourceCode.cs
File metadata and controls
105 lines (95 loc) · 4.37 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//-----------------------------------------------------------------------
// <copyright file="PolicySourceCode.cs" company="Microsoft">
// All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using Microsoft.PowerPlatform.ConnectorPlatform.Azure.ApimPolicyContext;
using Newtonsoft.Json.Linq;
namespace ConnectorManager.Resources.Basic
{
// This class is not used in this project.
// The CSharp in here will be embedded in the policy. It will be html escaped before added to the policy.
internal class PolicySourceCode : PolicyCodeBase
{
public string GetAccessTokenKey()
{
var connectionId = (string)context.Variables["connectionId"];
return "access_token" + connectionId;
}
public bool IsAccessTokenNull()
{
// return !context.Variables.ContainsKey("AccessToken") || context.Variables["AccessToken"] == null;
return true;
}
public string GetTokenBody()
{
if (!context.Variables.ContainsKey("tokens")
|| ((JObject)context.Variables["tokens"]).GetValue("key", StringComparison.OrdinalIgnoreCase) == null
|| ((JObject)context.Variables["tokens"]).GetValue("secret", StringComparison.OrdinalIgnoreCase) == null)
{
throw new Exception("EXPECTED400:Couldn't access login credentials. Check your connection to EightFold.");
}
const string grantType = "password";
var tokens = (JObject)context.Variables["tokens"];
var key = (string)tokens.GetValue("key", StringComparison.OrdinalIgnoreCase);
var secret = (string)tokens.GetValue("secret", StringComparison.OrdinalIgnoreCase);
return $"grant_type={grantType}&username={key}&password={secret}";
}
public string GetAuthorizationToken()
{
if (!context.Variables.ContainsKey("tokens")
|| ((JObject)context.Variables["tokens"]).GetValue("key", StringComparison.OrdinalIgnoreCase) == null
|| ((JObject)context.Variables["tokens"]).GetValue("Authorization", StringComparison.OrdinalIgnoreCase) == null
|| ((JObject)context.Variables["tokens"]).GetValue("secret", StringComparison.OrdinalIgnoreCase) == null)
{
throw new Exception("EXPECTED400:Couldn't access login credentials. Check your connection to EightFold.");
}
var tokens = (JObject)context.Variables["tokens"];
var auth = (string)tokens.GetValue("Authorization", StringComparison.OrdinalIgnoreCase);
return auth;
}
public string GetAccessToken()
{
if (context.Variables["IssueTokenResponse"] == null
|| ((IResponse)context.Variables["IssueTokenResponse"]).Body == null
|| ((IResponse)context.Variables["IssueTokenResponse"]).Body.As<JObject>(true).GetValue("access_token", StringComparison.OrdinalIgnoreCase) == null)
{
throw new Exception("EXPECTED401: Error; Couldn't authenticate request.");
}
var response = ((IResponse)context.Variables["IssueTokenResponse"]).Body.As<JObject>(true);
var tokenValue = response.GetValue("access_token", StringComparison.OrdinalIgnoreCase);
return "Bearer " + tokenValue;
}
public string RetrieveAccessToken()
{
return context.Variables["AccessToken"] as string;
}
public bool IsSuccessfulResponse()
{
int statusCode = context.Response.StatusCode;
return statusCode >= 200 && statusCode < 300;
}
public bool GetTokenFailed()
{
try
{
var response = (IResponse)context.Variables["IssueTokenResponse"];
var statusCode = response.StatusCode;
return !(statusCode >= 200 && statusCode < 300);
}
catch
{
throw new Exception("EXPECTED500: Unable to find account");
}
}
public string ProcessErrorAccessToken()
{
return "EXPECTED401:PB-APIM-ERR-0401,Invalid API key or secret";
}
}
}