-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVismaNetAuthenticationHelper.cs
More file actions
95 lines (91 loc) · 4.71 KB
/
VismaNetAuthenticationHelper.cs
File metadata and controls
95 lines (91 loc) · 4.71 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace VismaNetIntegrations
{
public static class VismaNetAuthenticationHelper
{
private const string TokenEndpoint = "security/api/v2/token";
/// <summary>
/// Creates a new token to use with the classic authentication flow
/// </summary>
/// <param name="client_id">ISV Client Id provided by Visma</param>
/// <param name="secret">ISV Client Secret provided by Visma</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <returns></returns>
public static async Task<string> CreateToken(string client_id, string secret, string username, string password)
{
var client = VismaNetClientBase.HttpClient;
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", client_id),
new KeyValuePair<string, string>("client_secret", secret),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("grant_type", "password")
});
var data = await client.PostAsync(TokenEndpoint, content);
var status = data.StatusCode;
if (status != HttpStatusCode.OK)
throw new VismaNetException("Failure to create token.", data.StatusCode.ToString(),
await data.Content.ReadAsStringAsync(), data.Headers.ToDictionary(x => x.Key, x => x.Value), null);
var rsp = JsonConvert.DeserializeObject<JObject>(await data.Content.ReadAsStringAsync());
return rsp["token"].Value<string>();
}
/// <summary>
/// Creates a new token using the OAuth authentication flow
/// </summary>
/// <param name="client_id">ISV Client Id provided by Visma</param>
/// <param name="secret">ISV Client Secret provided by Visma</param>
/// <param name="code"></param>
/// <param name="redirect_uri"></param>
/// <returns></returns>
public static async Task<string> CreateTokenFromCode(string client_id, string secret, string code,
string redirect_uri)
{
var client = VismaNetClientBase.HttpClient;
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", client_id),
new KeyValuePair<string, string>("client_secret", secret),
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("redirect_uri", redirect_uri),
new KeyValuePair<string, string>("grant_type", "authorization_code")
});
var data = await client.PostAsync(TokenEndpoint, content);
var status = data.StatusCode;
if (status != HttpStatusCode.OK)
throw new VismaNetException("Failure to create token from code.", data.StatusCode.ToString(),
await data.Content.ReadAsStringAsync(), data.Headers.ToDictionary(x => x.Key, x => x.Value), null);
var rsp = JsonConvert.DeserializeObject<JObject>(await data.Content.ReadAsStringAsync());
return rsp["token"].Value<string>();
}
/// <summary>
/// Create a url for OAuth authentication flow.
/// </summary>
/// <param name="client_id">ISV Client Id provided by Visma</param>
/// <param name="redirect_uri">Uri to redirect to after authentication</param>
/// <param name="state">State that will be appended to the callback</param>
/// <returns>Redirect Uri for OAuth authentication flow</returns>
public static string CreateOAuthUri(string client_id, string redirect_uri, string state = default(string))
{
if (string.IsNullOrEmpty(client_id))
throw new ArgumentException(nameof(client_id));
if (string.IsNullOrEmpty(redirect_uri))
throw new ArgumentException(nameof(redirect_uri));
return
$"{VismaNetClientBase.ApiBaseUrl}resources/oauth/authorize" +
$"?response_type=code" +
$"&client_id={client_id}" +
$"&scope=financialstasks" +
$"&redirect_uri={Uri.EscapeDataString(redirect_uri)}" +
$"&state={state ?? Guid.NewGuid().ToString()}";
}
}
}