Skip to content

Commit 8176134

Browse files
committed
Merge pull request #30 from timhall/authenticator-update
Add AfterExecute hook for IAuthenticator
2 parents 1073924 + d3913ee commit 8176134

11 files changed

+312
-65
lines changed

authenticators/EmptyAuthenticator.cls

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,69 @@ Attribute VB_GlobalNameSpace = False
77
Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = False
10+
''
11+
' Base for setting up authenticator
12+
'
13+
' @author:
14+
' @license:
15+
' @implements: IAuthenticator
16+
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
1017
Implements IAuthenticator
18+
Option Explicit
19+
20+
' ============================================= '
21+
' Public Methods
22+
' ============================================= '
23+
24+
''
25+
' Setup authenticator
26+
' --------------------------------------------- '
1127

1228
Public Sub Setup()
1329
' Define any user-specific variables needed for authentication
1430
End Sub
1531

16-
Private Sub IAuthenticator_BeforeExecute(Request As RestRequest)
17-
' (Used to add any required fields to the `Request` before it is executed)
32+
' ============================================= '
33+
' Private Methods
34+
' ============================================= '
35+
36+
''
37+
' Hook for taking action before a request is executed
38+
'
39+
' @param {RestClient} Client The client that is about to execute the request
40+
' @param {RestRequest} Request The request about to be executed
41+
' --------------------------------------------- '
42+
43+
Private Sub IAuthenticator_BeforeExecute(ByVal Client As RestClient, ByRef Request As RestRequest)
44+
' Add headers, cookies, etc to `Request` before it is executed
45+
' (Leave blank to pass Request through unmodified)
46+
End Sub
47+
48+
''
49+
' Hook for taking action after request has been executed
50+
'
51+
' @param {RestClient} Client The client that executed request
52+
' @param {RestRequest} Request The request that was just executed
53+
' @param {RestResponse} Response to request
54+
' --------------------------------------------- '
55+
56+
Private Sub IAuthenticator_AfterExecute(ByVal Client As RestClient, ByVal Request As RestRequest, ByRef Response As RestResponse)
1857

19-
' Leave blank to pass Request through unmodified
2058
End Sub
2159

22-
Private Sub IAuthenticator_HttpOpen( _
23-
Http As Object, _
24-
Request As RestRequest, _
25-
BaseUrl As String, _
26-
Optional UseAsync As Boolean = False)
27-
' (Used to open the given http request, making any necessary modifications)
60+
''
61+
' Hook for overriding standard http open (used for HTTP Basic)
62+
'
63+
' @param {MSXML2.IXMLHTTPRequest} http
64+
' @parma {RestClient} Client The client that is about to open request
65+
' @param {RestRequest} Request The request about to be opened
66+
' @param {String} BaseUrl
67+
' @param {Boolean} [useAsync=False]
68+
' --------------------------------------------- '
69+
70+
Private Sub IAuthenticator_HttpOpen(ByRef Http As Object, ByVal Client As RestClient, ByRef Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
71+
' Use modified http open (if necessary)
2872

2973
' Perform standard http open
30-
' Call http.Open(Request.MethodName(), Request.FullUrl(BaseUrl), useAsync)
74+
Call Http.Open(Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync)
3175
End Sub

authenticators/GoogleAuthenticator.cls

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ Attribute VB_GlobalNameSpace = False
77
Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
10-
Implements IAuthenticator
1110
''
12-
' Google Authenticator v1.0.0
11+
' Google Authenticator v2.0.0
1312
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1413
'
1514
' Custom IAuthenticator for "installed application" authentication for Google APIs
1615
'
1716
' - https://developers.google.com/accounts/docs/OAuth2#installed
1817
' - https://developers.google.com/accounts/docs/OAuth2InstalledApp
1918
'
20-
' @dependencies
19+
' @implements IAuthenticator
2120
2221
' @license: MIT (http://www.opensource.org/licenses/mit-license.php)
2322
'
2423
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
24+
Implements IAuthenticator
25+
Option Explicit
2526

2627
Private Const AuthorizationUrl As String = "https://accounts.google.com/o/oauth2/auth"
2728
Private Const RedirectUri As String = "urn:ietf:wg:oauth:2.0:oob"
@@ -111,6 +112,10 @@ End Property
111112
' Public Methods
112113
' ============================================= '
113114

115+
''
116+
' Setup authenticator
117+
' --------------------------------------------- '
118+
114119
Public Sub Setup(ClientId As String, ClientSecret As String)
115120
Me.ClientId = ClientId
116121
Me.ClientSecret = ClientSecret
@@ -148,21 +153,46 @@ Public Sub Logout()
148153
Me.Token = ""
149154
End Sub
150155

151-
Private Sub IAuthenticator_BeforeExecute(Request As RestRequest)
156+
''
157+
' Hook for taking action before a request is executed
158+
'
159+
' @param {RestClient} Client The client that is about to execute the request
160+
' @param {RestRequest} Request The request about to be executed
161+
' --------------------------------------------- '
162+
163+
Private Sub IAuthenticator_BeforeExecute(ByVal Client As RestClient, ByRef Request As RestRequest)
152164
If Me.ApiKey <> "" Then
153165
Request.AddQuerystringParam "key", Me.ApiKey
154166
Else
155167
Request.AddHeader "Authorization", "Bearer " & Me.Token
156168
End If
157169
End Sub
158170

159-
Private Sub IAuthenticator_HttpOpen( _
160-
Http As Object, _
161-
Request As RestRequest, _
162-
BaseUrl As String, _
163-
Optional UseAsync As Boolean = False)
171+
''
172+
' Hook for taking action after request has been executed
173+
'
174+
' @param {RestClient} Client The client that executed request
175+
' @param {RestRequest} Request The request that was just executed
176+
' @param {RestResponse} Response to request
177+
' --------------------------------------------- '
178+
179+
Private Sub IAuthenticator_AfterExecute(ByVal Client As RestClient, ByVal Request As RestRequest, ByRef Response As RestResponse)
180+
181+
End Sub
182+
183+
''
184+
' Hook for overriding standard http open (used for HTTP Basic)
185+
'
186+
' @param {MSXML2.IXMLHTTPRequest} http
187+
' @parma {RestClient} Client The client that is about to open request
188+
' @param {RestRequest} Request The request about to be opened
189+
' @param {String} BaseUrl
190+
' @param {Boolean} [useAsync=False]
191+
' --------------------------------------------- '
192+
193+
Private Sub IAuthenticator_HttpOpen(ByRef Http As Object, ByVal Client As RestClient, ByRef Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
164194

165-
' Perform standard http open
195+
' Perform standard http open
166196
Http.Open Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync
167197
End Sub
168198

authenticators/HttpBasicAuthenticator.cls

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
Implements IAuthenticator
1111
''
12-
' HttpBasicAuthenticator v1.0.0
12+
' HttpBasicAuthenticator v2.0.0
1313
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1414
'
1515
' Utilize http basic authentication
1616
'
17-
17+
1818
' @license: MIT (http://www.opensource.org/licenses/mit-license.php)
1919
'
2020
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
@@ -48,11 +48,40 @@ End Sub
4848
' Private Methods
4949
' ============================================= '
5050

51-
Private Sub IAuthenticator_BeforeExecute(Request As RestRequest)
51+
''
52+
' Hook for taking action before a request is executed
53+
'
54+
' @param {RestClient} Client The client that is about to execute the request
55+
' @param {RestRequest} Request The request about to be executed
56+
' --------------------------------------------- '
57+
58+
Private Sub IAuthenticator_BeforeExecute(ByVal Client As RestClient, ByRef Request As RestRequest)
5259
Request.AddHeader "Authorization", CreateHeader()
5360
End Sub
5461

55-
Private Sub IAuthenticator_HttpOpen(Http As Object, Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
62+
''
63+
' Hook for taking action after request has been executed
64+
'
65+
' @param {RestClient} Client The client that executed request
66+
' @param {RestRequest} Request The request that was just executed
67+
' @param {RestResponse} Response to request
68+
' --------------------------------------------- '
69+
70+
Private Sub IAuthenticator_AfterExecute(ByVal Client As RestClient, ByVal Request As RestRequest, ByRef Response As RestResponse)
71+
72+
End Sub
73+
74+
''
75+
' Hook for overriding standard http open (used for HTTP Basic)
76+
'
77+
' @param {MSXML2.IXMLHTTPRequest} http
78+
' @parma {RestClient} Client The client that is about to open request
79+
' @param {RestRequest} Request The request about to be opened
80+
' @param {String} BaseUrl
81+
' @param {Boolean} [useAsync=False]
82+
' --------------------------------------------- '
83+
84+
Private Sub IAuthenticator_HttpOpen(ByRef Http As Object, ByVal Client As RestClient, ByRef Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
5685
' Use http open with username and password values set
5786
' (This is used in addition to setting request header, as some services required this)
5887
Http.Open Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync, Me.Username, Me.Password

authenticators/OAuth1Authenticator.cls

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
Implements IAuthenticator
1111
''
12-
' OAuth1 Authenticator v1.0.0
12+
' OAuth1 Authenticator v2.0.0
1313
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1414
'
1515
' Utilize OAuth1 authentication
1616
'
1717
' @dependencies
1818
' Microsoft XML, v3+
19-
19+
2020
' @license: MIT (http://www.opensource.org/licenses/mit-license.php)
2121
'
2222
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
@@ -98,14 +98,43 @@ End Sub
9898
' Private Methods
9999
' ============================================= '
100100

101-
Private Sub IAuthenticator_BeforeExecute(Request As RestRequest)
101+
''
102+
' Hook for taking action before a request is executed
103+
'
104+
' @param {RestClient} Client The client that is about to execute the request
105+
' @param {RestRequest} Request The request about to be executed
106+
' --------------------------------------------- '
107+
108+
Private Sub IAuthenticator_BeforeExecute(ByVal Client As RestClient, ByRef Request As RestRequest)
102109
' Add authorization header to request
103110
Request.AddHeader "Authorization", CreateHeader(Request)
104111
End Sub
105112

106-
Private Sub IAuthenticator_HttpOpen(Http As Object, Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
107-
' Standard http open
108-
Http.Open Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync
113+
''
114+
' Hook for taking action after request has been executed
115+
'
116+
' @param {RestClient} Client The client that executed request
117+
' @param {RestRequest} Request The request that was just executed
118+
' @param {RestResponse} Response to request
119+
' --------------------------------------------- '
120+
121+
Private Sub IAuthenticator_AfterExecute(ByVal Client As RestClient, ByVal Request As RestRequest, ByRef Response As RestResponse)
122+
123+
End Sub
124+
125+
''
126+
' Hook for overriding standard http open (used for HTTP Basic)
127+
'
128+
' @param {MSXML2.IXMLHTTPRequest} http
129+
' @parma {RestClient} Client The client that is about to open request
130+
' @param {RestRequest} Request The request about to be opened
131+
' @param {String} BaseUrl
132+
' @param {Boolean} [useAsync=False]
133+
' --------------------------------------------- '
134+
135+
Private Sub IAuthenticator_HttpOpen(ByRef Http As Object, ByVal Client As RestClient, ByRef Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
136+
' Perform standard http open
137+
Call Http.Open(Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync)
109138
End Sub
110139

111140
Private Function CreateHeader(Request As RestRequest) As String

authenticators/OAuth2Authenticator.cls

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
Implements IAuthenticator
1111
''
12-
' OAuth2 Authenticator v1.0.0
12+
' OAuth2 Authenticator v2.0.0
1313
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1414
'
1515
' Utilize OAuth2 authentication
1616
' (Currently using client credentials flow only)
1717
'
1818
' @dependencies
1919
' Microsoft XML, v3+
20-
20+
2121
' @license: MIT (http://www.opensource.org/licenses/mit-license.php)
2222
'
2323
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
@@ -68,15 +68,22 @@ Public Sub SetupTokenUrl(TokenUrl As String, Optional TokenKey As String = "acce
6868
Me.TokenKey = TokenKey
6969
End Sub
7070

71-
Private Sub IAuthenticator_BeforeExecute(Request As RestRequest)
71+
''
72+
' Hook for taking action before a request is executed
73+
'
74+
' @param {RestClient} Client The client that is about to execute the request
75+
' @param {RestRequest} Request The request about to be executed
76+
' --------------------------------------------- '
77+
78+
Private Sub IAuthenticator_BeforeExecute(ByVal Client As RestClient, ByRef Request As RestRequest)
7279
On Error GoTo ErrorHandling
7380
If (Me.Token = "" Or Not Me.CacheToken) And (Me.TokenUrl <> "" And Me.TokenKey <> "") Then
7481
' Get new token
7582
Dim Http As Object
7683
Set Http = CreateObject("MSXML2.ServerXMLHTTP")
7784

7885
Http.Open "POST", CreateTokenRequest, False
79-
Http.send
86+
Http.Send
8087

8188
If Http.Status <> 200 Then
8289
' Error getting OAuth2 token
@@ -113,9 +120,31 @@ ErrorHandling:
113120
End If
114121
End Sub
115122

116-
Private Sub IAuthenticator_HttpOpen(Http As Object, Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
123+
''
124+
' Hook for taking action after request has been executed
125+
'
126+
' @param {RestClient} Client The client that executed request
127+
' @param {RestRequest} Request The request that was just executed
128+
' @param {RestResponse} Response to request
129+
' --------------------------------------------- '
130+
131+
Private Sub IAuthenticator_AfterExecute(ByVal Client As RestClient, ByVal Request As RestRequest, ByRef Response As RestResponse)
132+
133+
End Sub
134+
135+
''
136+
' Hook for overriding standard http open (used for HTTP Basic)
137+
'
138+
' @param {MSXML2.IXMLHTTPRequest} http
139+
' @parma {RestClient} Client The client that is about to open request
140+
' @param {RestRequest} Request The request about to be opened
141+
' @param {String} BaseUrl
142+
' @param {Boolean} [useAsync=False]
143+
' --------------------------------------------- '
144+
145+
Private Sub IAuthenticator_HttpOpen(ByRef Http As Object, ByVal Client As RestClient, ByRef Request As RestRequest, BaseUrl As String, Optional UseAsync As Boolean = False)
117146
' Perform standard http open
118-
Http.Open Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync
147+
Call Http.Open(Request.MethodName(), Request.FullUrl(BaseUrl), UseAsync)
119148
End Sub
120149

121150
' ============================================= '

0 commit comments

Comments
 (0)