This repository was archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathServiceProvider.cs
More file actions
98 lines (83 loc) · 3.56 KB
/
ServiceProvider.cs
File metadata and controls
98 lines (83 loc) · 3.56 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
using System;
using System.Configuration;
using Booyami.DevDefined.OAuth.Consumer;
using Booyami.DevDefined.OAuth.Storage.Basic;
using XeroApi;
using XeroApi.OAuth;
namespace Xero.ScreencastWeb.Services
{
public class ServiceProvider
{
[ThreadStatic]
private static IOAuthSession _oauthSession;
[ThreadStatic]
private static ITokenRepository _tokenRepository;
/// <summary>
/// Gets the current instance of Repository (used for getting data from the Xero API)
/// </summary>
/// <returns></returns>
public static Repository GetCurrentRepository()
{
var session = GetCurrentSession();
return (session != null)
? new Repository(session)
: null;
}
/// <summary>
/// Gets the current OAuthSession (used for getting request tokens, access tokens, etc.)
/// </summary>
/// <returns></returns>
public static IOAuthSession GetCurrentSession()
{
return _oauthSession ?? (_oauthSession = CreateOAuthSession());
}
/// <summary>
/// Creates the OAuth session.
/// </summary>
/// <returns></returns>
private static IOAuthSession CreateOAuthSession()
{
const string userAgent = "Xero.API.ScreenCastWeb v2.0";
if (ConfigurationManager.AppSettings["XeroApiSignatureMethod"] == "HMAC-SHA1")
{
// Public App
return new XeroApiPublicSession(
userAgent,
ConfigurationManager.AppSettings["XeroApiConsumerKey"], // Consumer Key
ConfigurationManager.AppSettings["XeroApiConsumerSecret"], // Consumer Secret
CurrentTokenRepository); // Token Repository
}
if (ConfigurationManager.AppSettings["XeroApiSignatureMethod"] == "RSA-SHA1")
{
if (ConfigurationManager.AppSettings["XeroApiBaseUrl"].ToLower().IndexOf("partner") > 0)
{
// Partner App
return new XeroApiPartnerSession(
userAgent,
ConfigurationManager.AppSettings["XeroApiConsumerKey"], // Consumer Key
CertificateRepository.GetOAuthSigningCertificate(), // OAuth Signing Certificate
CertificateRepository.GetClientSslCertificate(), // Client SSL Certificate
CurrentTokenRepository); // Token Repository
}
else
{
// Private App
return new XeroApiPrivateSession(
userAgent,
ConfigurationManager.AppSettings["XeroApiConsumerKey"], // Consumer Key
CertificateRepository.GetOAuthSigningCertificate()); // OAuth Signing Certificate
}
}
throw new ConfigurationErrorsException("The configuration for a Public/Private/Partner app cannot be determined.");
}
/// <summary>
/// Gets or sets the current token repository.
/// </summary>
/// <value>The current token repository.</value>
public static ITokenRepository CurrentTokenRepository
{
get { return _tokenRepository; }
set { _tokenRepository = value; }
}
}
}