Skip to content

Commit a196385

Browse files
authored
Merge pull request #179 from umbraco/bugfix/semrush-auth-ui-status
Bugfix/semrush auth UI status
2 parents 8515d46 + 82f05d0 commit a196385

File tree

9 files changed

+44
-11
lines changed

9 files changed

+44
-11
lines changed

src/Umbraco.Cms.Integrations.SEO.Semrush/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/semrush.controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
function validateToken() {
201201
umbracoCmsIntegrationsSemrushResource.validateToken().then(function (response) {
202202
vm.isFreeAccount = response.isFreeAccount;
203-
if (response.isExpired) {
203+
if (!response.isAuthorized) {
204204
vm.isConnected = false;
205205
} else {
206206
if (response.isValid === false) {
@@ -275,6 +275,7 @@
275275
var options = {
276276
title: "SEMrush Authorization Details",
277277
isFreeAccount: vm.isFreeAccount,
278+
isAuthorized: vm.isConnected,
278279
view: "/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/statusEditor.html",
279280
size: "small",
280281
revoke: function () {

src/Umbraco.Cms.Integrations.SEO.Semrush/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/status.controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
var vm = this;
66

7-
vm.isFreeAccount = $scope.model.isFreeAccount !== null
7+
vm.isAuthorized = $scope.model.isAuthorized;
8+
vm.isFreeAccount = $scope.model.isFreeAccount !== null && $scope.model.isAuthorized
89
? ($scope.model.isFreeAccount === true ? "Free" : "Paid") : "N.A.";
910

1011
umbracoCmsIntegrationsSemrushResource.getTokenDetails().then(function (response) {

src/Umbraco.Cms.Integrations.SEO.Semrush/App_Plugins/UmbracoCms.Integrations/SEO/Semrush/statusEditor.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<umb-editor-container>
55
<umb-box>
66
<umb-box-content>
7-
<p><b>Connected:</b> {{ vm.isAccessTokenAvailable }}</p>
7+
<p><b>Connected:</b> {{ vm.isAccessTokenAvailable && vm.isAuthorized }}</p>
88
<p><b>Account:</b> {{ vm.isFreeAccount }}</p>
99
<p ng-if="vm.isAccessTokenAvailable"><b>Access Token:</b> {{ vm.AccessToken }}</p>
1010
<umb-button action="vm.onRevokeToken()"

src/Umbraco.Cms.Integrations.SEO.Semrush/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class Constants
1111

1212
public const string SemrushKeywordsEndpoint = "{0}api/v1/keywords/{1}?access_token={2}&phrase={3}&database={4}";
1313

14+
public const string BadRefreshToken = "BAD_REFRESH_TOKEN";
15+
1416
public static class Configuration
1517
{
1618
public const string Settings = "Umbraco:Cms:Integrations:SEO:Semrush:Settings";

src/Umbraco.Cms.Integrations.SEO.Semrush/Controllers/SemrushController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,19 @@ public async Task<AuthorizationResponseDto> ValidateToken()
113113
{
114114
_semrushTokenService.TryGetParameters(Constants.TokenDbKey, out TokenDto token);
115115

116-
if (!token.IsAccessTokenAvailable) return new AuthorizationResponseDto { IsExpired = true };
116+
if (!token.IsAccessTokenAvailable) return new AuthorizationResponseDto { IsAuthorized = false };
117117

118118
var response = await ClientFactory()
119119
.GetAsync(string.Format(Constants.SemrushKeywordsEndpoint, _settings.BaseUrl, "phrase_related", token.AccessToken, "ping", "us"));
120120

121+
if (response.StatusCode == HttpStatusCode.Unauthorized)
122+
{
123+
await _authorizationService.RefreshAccessTokenAsync();
124+
}
125+
121126
return new AuthorizationResponseDto
122127
{
128+
IsAuthorized = response.StatusCode == HttpStatusCode.OK,
123129
IsValid = response.StatusCode != HttpStatusCode.Unauthorized,
124130
IsFreeAccount = response.Headers.TryGetValues(Constants.AllowLimitOffsetHeaderName,
125131
out IEnumerable<string> values)

src/Umbraco.Cms.Integrations.SEO.Semrush/Models/Dtos/AuthorizationResponseDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace Umbraco.Cms.Integrations.SEO.Semrush.Models.Dtos
55
{
66
public class AuthorizationResponseDto
77
{
8-
[JsonProperty("isExpired")]
9-
public bool IsExpired { get; set; }
8+
[JsonProperty("isAuthorized")]
9+
public bool IsAuthorized { get; set; }
1010

1111
[JsonProperty("isValid")]
1212
public bool IsValid { get; set; }

src/Umbraco.Cms.Integrations.SEO.Semrush/Services/AuthorizationService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using Umbraco.Cms.Integrations.SEO.Semrush.Configuration;
77
using System.Net.Http;
88
using Umbraco.Cms.Integrations.SEO.Semrush.Models.Dtos;
9+
using Newtonsoft.Json.Linq;
10+
11+
using Newtonsoft.Json;
12+
913

1014
#if NETCOREAPP
1115
using Microsoft.Extensions.Options;
@@ -97,6 +101,16 @@ public async Task<string> RefreshAccessTokenAsync()
97101

98102
return result;
99103
}
104+
else
105+
{
106+
var responseContent = await response.Content.ReadAsStringAsync();
107+
108+
var statusObject = (JObject)JsonConvert.DeserializeObject(responseContent);
109+
if (statusObject.ContainsKey("status") && statusObject["status"].ToString() == Constants.BadRefreshToken)
110+
{
111+
SemrushTokenService.RemoveParameters(Constants.TokenDbKey);
112+
}
113+
}
100114

101115
return "error";
102116
}

src/Umbraco.Cms.Integrations.SEO.Semrush/Services/UmbracoAuthorizationService.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
24
using System.Net.Http;
35
using System.Threading.Tasks;
46
using Umbraco.Cms.Integrations.SEO.Semrush.Models.Dtos;
@@ -70,6 +72,7 @@ public async Task<string> RefreshAccessTokenAsync()
7072
requestMessage.Headers.Add("service", "Semrush");
7173

7274
var response = await ClientFactory().SendAsync(requestMessage);
75+
7376
if (response.IsSuccessStatusCode)
7477
{
7578
var result = await response.Content.ReadAsStringAsync();
@@ -78,6 +81,16 @@ public async Task<string> RefreshAccessTokenAsync()
7881

7982
return result;
8083
}
84+
else
85+
{
86+
var responseContent = await response.Content.ReadAsStringAsync();
87+
88+
var statusObject = (JObject)JsonConvert.DeserializeObject(responseContent);
89+
if (statusObject.ContainsKey("status") && statusObject["status"].ToString() == Constants.BadRefreshToken)
90+
{
91+
SemrushTokenService.RemoveParameters(Constants.TokenDbKey);
92+
}
93+
}
8194

8295
return "error";
8396
}

src/Umbraco.Cms.Integrations.SEO.Semrush/Umbraco.Cms.Integrations.SEO.Semrush.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@
6161
</Content>
6262
</ItemGroup>
6363

64-
<ItemGroup>
65-
<Compile Remove="SemrushComponent.cs" />
66-
</ItemGroup>
67-
6864
<ItemGroup>
6965
<Content Include="semrush.png">
7066
<Pack>true</Pack>

0 commit comments

Comments
 (0)