forked from dracoon/dracoon-csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthExamples.cs
More file actions
105 lines (84 loc) · 4.22 KB
/
OAuthExamples.cs
File metadata and controls
105 lines (84 loc) · 4.22 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
using Dracoon.Sdk.Model;
using System;
using System.Net;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace Dracoon.Sdk.Example {
public class OAuthExamples {
private static readonly Uri SERVER_URI = new Uri("https://dracoon.team");
private static readonly string CLIENT_ID = "client-id";
private static readonly string CLIENT_SECRET = "client-secret";
private static readonly int REDIRECT_PORT = 10000;
[STAThread]
static void Main() {
// Authorize client
string authCode = AuthorizeClient();
// Initialize dracoon client
DracoonClient client = CreateClient(authCode);
// Use client
UseClient(client);
// After first usage of client the access and refresh tokens are retrieved and can be persisted
string accessToken = client.Auth.AccessToken;
string refreshToken = client.Auth.RefreshToken;
// On next startup of your application you can then use the tokens for another initialization without users browser login
client = CreateClient(accessToken, refreshToken);
// Use client again with new initialization
UseClient(client);
}
private static string AuthorizeClient() {
string state = GenerateRandomBase64(32);
// Create authorization uri
Uri authUrl = OAuthHelper.CreateAuthorizationUrl(SERVER_URI, CLIENT_ID, state);
// Open authorization URL in user's browser and wait for callback
Uri loginResultUri = Login(authUrl).Result;
// Extract the state and code from callback uri
string callbackState = OAuthHelper.ExtractAuthorizationStateFromUri(loginResultUri);
string callbackCode = OAuthHelper.ExtractAuthorizationCodeFromUri(loginResultUri);
// Check state
if (!state.Equals(callbackState)) {
throw new Exception("Received OAuth state is not the same as expected!");
}
return callbackCode;
}
private async static Task<Uri> Login(Uri authUrl) {
using (HttpListener callbackListener = new HttpListener()) {
callbackListener.Prefixes.Add("http://localhost:" + REDIRECT_PORT + "/");
callbackListener.Start();
System.Diagnostics.Process.Start(authUrl.ToString());
HttpListenerContext context = await callbackListener.GetContextAsync();
return context.Request.Url;
}
}
private static DracoonClient CreateClient(string authCode) {
// Init the auth object with the authCode
DracoonAuth auth = new DracoonAuth(CLIENT_ID, CLIENT_SECRET, authCode);
// Create a dracoon client with default settings but with authorization
return new DracoonClient(SERVER_URI, auth);
}
private static DracoonClient CreateClient(string accessToken, string refreshToken) {
// Init the auth object with the access and refresh token
DracoonAuth auth = new DracoonAuth(CLIENT_ID, CLIENT_SECRET, accessToken, refreshToken);
// Create a dracoon client with default settings but with authorization
return new DracoonClient(SERVER_URI, auth);
}
private static void UseClient(DracoonClient client) {
NodeList rootNodes = client.Nodes.GetNodes();
foreach (Node current in rootNodes.Items) {
Console.WriteLine("NodeId: " + current.Id + "; NodeName: " + current.Name);
}
}
private static string GenerateRandomBase64(int length) {
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomBytes = new byte[length];
rng.GetBytes(randomBytes);
return EncodeByteArrayToUrlEncodedBase64(randomBytes);
}
internal static string EncodeByteArrayToUrlEncodedBase64(byte[] bytes) {
string base64 = Convert.ToBase64String(bytes);
base64 = base64.Replace("+", "-");
base64 = base64.Replace("/", "_");
base64 = base64.Replace("=", "");
return base64;
}
}
}