Skip to content

Commit f33e8c8

Browse files
authored
Merge pull request #74 from umbraco/v10/feature/hubspot-private-access-token
HubSpot support for Private Access Tokens.
2 parents 2e4f1df + 52c3820 commit f33e8c8

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/Umbraco.Forms.Integrations.Crm.Hubspot/App_Plugins/UmbracoForms.Integrations/Crm/Hubspot/hubspot-field-mapper-template.html

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<div ng-show="!vm.loading && vm.authorizationStatus === 'Unauthenticated'">
88
<p>Umbraco Forms is not configured with a HubSpot CRM account.</p>
9-
<p>To do this you can either create and save an API key into the <i>UmbracoForms.config</i> file.</p>
9+
<p>To do this you can either create and save an API key or a Private Access Token into the <i>appsettings.json</i> file.</p>
1010
<p>Or you can click <a ng-click="vm.openAuth()" style="text-decoration: underline">here</a> to complete an OAuth connection.</p>
1111
<p><em>If your browser is unable to process the automated connection, paste the provided authorization code below and click to complete the authentication.</em>
1212
<input type="text" placeholder="Enter authorization code" ng-model="vm.authorizationCode" />
@@ -15,7 +15,11 @@
1515

1616
<div ng-show="!vm.loading && vm.authorizationStatus !== 'Unauthenticated'">
1717

18-
<div class="umb-forms-mappings" ng-show="vm.mappings.length > 0 && vm.hubspotFields.length > 0">
18+
<div class="umb-forms-settings-note ng-scope">
19+
Umbraco Forms is configured with a HubSpot CRM account using: <b>{{ vm.authorizationStatus }}</b></p>
20+
</div>
21+
22+
<div class="umb-forms-mappings mt2" ng-show="vm.mappings.length > 0 && vm.hubspotFields.length > 0">
1923

2024
<div class="umb-forms-mapping-header">
2125
<div class="umb-forms-mapping-field -no-margin-left">Form Field</div>
@@ -54,10 +58,12 @@
5458
</div>
5559
</div>
5660

57-
<umb-button type="button" action="vm.addMapping()" label="Add mapping"></umb-button>
61+
<div class="mt2">
62+
<umb-button type="button" action="vm.addMapping()" label="Add mapping"></umb-button>
5863

59-
<div ng-show="vm.authorizationStatus === 'OAuth'" style="margin-top: 20px">
60-
<umb-button type="button" action="vm.deauthorize()" label="De-authorize from Hubspot"></umb-button>
64+
<div ng-show="vm.authorizationStatus === 'OAuth'" style="margin-top: 20px">
65+
<umb-button type="button" action="vm.deauthorize()" label="De-authorize from Hubspot"></umb-button>
66+
</div>
6167
</div>
6268

6369
</div>

src/Umbraco.Forms.Integrations.Crm.Hubspot/Configuration/HubspotSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public class HubspotSettings
55
{
66
public string ApiKey { get; set; }
77

8+
public string PrivateAccessToken { get; set; }
9+
810
public bool AllowContactUpdate { get; set; }
911
}
1012
}

src/Umbraco.Forms.Integrations.Crm.Hubspot/Services/AuthenticationDetail.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,37 @@ public enum AuthenticationMode
44
{
55
Unauthenticated,
66
ApiKey,
7+
PrivateAccessToken,
78
OAuth
89
}
910

1011
public class AuthenticationDetail
1112
{
1213
public string ApiKey { get; set; }
1314

15+
public string PrivateAccessToken { get; set; }
16+
1417
public string RefreshToken { get; set; }
1518

16-
public AuthenticationMode Mode =>
17-
!string.IsNullOrEmpty(ApiKey)
18-
? AuthenticationMode.ApiKey
19-
: !string.IsNullOrEmpty(RefreshToken)
20-
? AuthenticationMode.OAuth
21-
: AuthenticationMode.Unauthenticated;
19+
public AuthenticationMode Mode
20+
{
21+
get
22+
{
23+
if (string.IsNullOrEmpty(ApiKey)
24+
&& string.IsNullOrEmpty(PrivateAccessToken)
25+
&& string.IsNullOrEmpty(RefreshToken))
26+
return AuthenticationMode.Unauthenticated;
27+
28+
if(!string.IsNullOrEmpty(ApiKey))
29+
{
30+
return AuthenticationMode.ApiKey;
31+
} else if(!string.IsNullOrEmpty(PrivateAccessToken))
32+
{
33+
return AuthenticationMode.PrivateAccessToken;
34+
}
35+
36+
return AuthenticationMode.OAuth;
37+
}
38+
}
2239
}
2340
}

src/Umbraco.Forms.Integrations.Crm.Hubspot/Services/HubspotContactService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ private AuthenticationDetail GetConfiguredAuthenticationDetails()
273273
{
274274
authentication.ApiKey = apiKey;
275275
}
276+
else if (TryGetPrivateAccessToken(out string privateAccessToken))
277+
{
278+
authentication.PrivateAccessToken = privateAccessToken;
279+
}
276280
else if (TryGetSavedRefreshToken(out string refreshToken))
277281
{
278282
authentication.RefreshToken = refreshToken;
@@ -288,6 +292,13 @@ private bool TryGetApiKey(out string apiKey)
288292
return !string.IsNullOrEmpty(apiKey);
289293
}
290294

295+
private bool TryGetPrivateAccessToken(out string accessToken)
296+
{
297+
accessToken = _settings.PrivateAccessToken;
298+
299+
return !string.IsNullOrEmpty(accessToken);
300+
}
301+
291302
private bool TryGetSavedRefreshToken(out string refreshToken)
292303
{
293304
refreshToken = _keyValueService.GetValue(RefreshTokenDatabaseKey);
@@ -367,6 +378,10 @@ private async Task<HttpResponseMessage> GetResponse(
367378
case AuthenticationMode.ApiKey:
368379
requestMessage.RequestUri = new Uri($"{url}?hapikey={authenticationDetails.ApiKey}");
369380
break;
381+
case AuthenticationMode.PrivateAccessToken:
382+
requestMessage.Headers.Authorization =
383+
new AuthenticationHeaderValue("Bearer", authenticationDetails.PrivateAccessToken);
384+
break;
370385
case AuthenticationMode.OAuth:
371386
requestMessage.Headers.Authorization =
372387
new AuthenticationHeaderValue("Bearer",

0 commit comments

Comments
 (0)