forked from JustDerb/Tiltify-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.cs
83 lines (75 loc) · 3 KB
/
Auth.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
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
using System.Collections.Generic;
using System.Threading.Tasks;
using Tiltify.Models;
namespace Tiltify
{
public class Auth : ApiBase
{
public enum AuthGrantType
{
None,
ClientCredentials,
AuthorizationCode,
RefreshToken
}
public string GrantTypeToString(AuthGrantType grantType)
{
switch(grantType)
{
default:
case AuthGrantType.None:
return "";
case AuthGrantType.ClientCredentials:
return "client_credentials";
case AuthGrantType.AuthorizationCode:
return "authorization_code";
case AuthGrantType.RefreshToken:
return "refresh_token";
}
}
public Auth(ApiSettings settings, IRateLimiter rateLimiter, IHttpCallHandler http) : base(settings, rateLimiter, http)
{
}
public Task<AuthorizationResponse> Authorize(AuthGrantType grantType = AuthGrantType.ClientCredentials, ApiAccessLevel scopes = ApiAccessLevel.Public, string authCode = "")
{
List<KeyValuePair<string, string>> Args = new List<KeyValuePair<string, string>>();
Args.Add(new KeyValuePair<string, string>("grant_type", GrantTypeToString(grantType)));
Args.Add(new KeyValuePair<string, string>("client_id", settings.ClientID));
Args.Add(new KeyValuePair<string, string>("client_secret", settings.ClientSecret));
if (grantType == AuthGrantType.AuthorizationCode)
{
Args.Add(new KeyValuePair<string, string>("code", authCode));
}
else if (grantType == AuthGrantType.RefreshToken)
{
Args.Add(new KeyValuePair<string, string>("refresh_token", settings.RefreshToken));
}
// Adding the scopes
string scopeForm;
switch(scopes)
{
default:
case ApiAccessLevel.Public:
scopeForm = "public";
break;
case ApiAccessLevel.Private:
scopeForm = "webhooks:write";
break;
case ApiAccessLevel.PublicPrivate:
scopeForm = "public webhooks:write";
break;
}
Args.Add(new KeyValuePair<string, string>("scope", scopeForm));
Task<AuthorizationResponse> authResp = TiltifyPostGenericAsync<AuthorizationResponse>("/oauth/token", GetApiVersion(), Args, null, ApiAccessLevel.OAuth);
if (authResp.Result != null)
{
AuthorizationResponse resp = authResp.Result;
if (resp.Type == "bearer")
settings.OAuthToken = resp.AccessToken;
if (resp.RefreshToken != null)
settings.RefreshToken = resp.RefreshToken;
}
return authResp;
}
}
}