Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JWT support for Simple JWT Login #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions WordPressPCL/Client/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public async Task RequestJWTokenAsync(string username, string password) {
_httpHelper.HttpResponsePreProcessing = null;
_httpHelper.JWToken = jwtResponse?.Data?.Token;
break;
case JWTPlugin.JWTSimpleJwtLogin:
var (jwtSimpleJwtLogin, _) = await _httpHelper.PostRequestAsync<JWTSimpleJwtLogin>("?rest_route=/simple-jwt-login/v1/auth", formContent, isAuthRequired: false, ignoreDefaultPath: true).ConfigureAwait(false);
_httpHelper.JWToken = jwtSimpleJwtLogin?.Data.Token;
break;
default:
throw new WPException("Invalid JWT Plugin");
}
Expand Down Expand Up @@ -134,6 +138,9 @@ public async Task<bool> IsValidJWTokenAsync() {
var (jwtResponse, _) = await _httpHelper.PostRequestAsync<JWTResponse>(route, null, isAuthRequired: true, ignoreDefaultPath: true).ConfigureAwait(false);
_httpHelper.HttpResponsePreProcessing = null;
return jwtResponse.Success;
case JWTPlugin.JWTSimpleJwtLogin:
var (jwtSimpleJwtLogin, _) = await _httpHelper.PostRequestAsync<JWTSimpleJwtLoginResponse>("?rest_route=/simple-jwt-login/v1/auth/validate", null, isAuthRequired: true, ignoreDefaultPath: true).ConfigureAwait(false);
return jwtSimpleJwtLogin.Success;
default:
throw new WPException("Invalid JWT Plugin");
}
Expand Down
14 changes: 10 additions & 4 deletions WordPressPCL/Models/JWTPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace WordPressPCL.Models {
namespace WordPressPCL.Models {

/// <summary>
/// JWT Plugins supported
Expand All @@ -14,7 +14,13 @@ public enum JWTPlugin {
/// JWT Auth – WordPress JSON Web Token Authentication plugin
/// Author - Useful Team
/// </summary>
JWTAuthByUsefulTeam

JWTAuthByUsefulTeam,
/// <summary>
/// simple-jwt-login – WordPress JSON Web Token Authentication plugin
/// https://wordpress.org/plugins/simple-jwt-login/
/// Author - Nicu Micle
/// </summary>
JWTSimpleJwtLogin

}
}
}
43 changes: 43 additions & 0 deletions WordPressPCL/Models/JWTSimpleJwtLogin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Newtonsoft.Json;

namespace WordPressPCL.Models
{
/// <summary>
/// Response class for the JWT Plugin
/// </summary>
public class JWTSimpleJwtLogin
{
/// <summary>
/// Indicates if the call was successful
/// </summary>
[JsonProperty("success")]
public bool Success { get; set; }

/// <summary>
/// The response message
/// </summary>
[JsonProperty("message")]
[DefaultValue(null)]
public string Message { get; set; }

/// <summary>
/// The JWT Content
/// </summary>
[JsonProperty("data")]
[DefaultValue(null)]
public JWTSimpleJwtLoginData Data { get; set; }
}

public class JWTSimpleJwtLoginData
{
/// <summary>
/// JWT Token
/// </summary>
[JsonProperty("jwt")]
public string Token { get; set; }
}
}
105 changes: 105 additions & 0 deletions WordPressPCL/Models/JWTSimpleJwtLoginResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Newtonsoft.Json;

namespace WordPressPCL.Models
{
public class JWTSimpleJwtLoginResponse
{
[JsonProperty("success")]
public bool Success { get; set; }

[JsonProperty("data")]
[DefaultValue(null)]
public JWTSimpleJwtLoginResponseData Data { get; set; }
}

public class JWTSimpleJwtLoginResponseData
{
[JsonProperty("user")]
public JWTSimpleJwtLoginResponseDataUser User { get; set; }

[JsonProperty("roles")]
public List<string> Roles { get; set; }

[JsonProperty("jwt")]
public List<JWTSimpleJwtLoginResponseDataJwt> Jwt { get; set; }
}

public class JWTSimpleJwtLoginResponseDataJwt
{
[JsonProperty("token")]
public string Token { get; set; }

[JsonProperty("header")]
public JWTSimpleJwtLoginResponseDataJwtHeader Header { get; set; }

[JsonProperty("payload")]
public JWTSimpleJwtLoginResponseDataJwtPayload Payload { get; set; }

[JsonProperty("expire_in")]
public int ExpireIn { get; set; }
}

public class JWTSimpleJwtLoginResponseDataJwtHeader
{
[JsonProperty("typ")]
public string Typ { get; set; }

[JsonProperty("alg")]
public string Alg { get; set; }
}

public class JWTSimpleJwtLoginResponseDataJwtPayload
{
[JsonProperty("iat")]
public int Iat { get; set; }

[JsonProperty("exp")]
public int Exp { get; set; }

[JsonProperty("email")]
public string Email { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("site")]
public string Site { get; set; }

[JsonProperty("username")]
public string Username { get; set; }
}

public class JWTSimpleJwtLoginResponseDataUser
{
[JsonProperty("ID")]
public string Id { get; set; }

[JsonProperty("user_login")]
public string UserLogin { get; set; }

[JsonProperty("user_nicename")]
public string UserNicename { get; set; }

[JsonProperty("user_email")]
public string UserEmail { get; set; }

[JsonProperty("user_url")]
public string UserUrl { get; set; }

[JsonProperty("user_registered")]
public string UserRegistered { get; set; }

[JsonProperty("user_activation_key")]
public string UserActivationKey { get; set; }

[JsonProperty("user_status")]
public string UserStatus { get; set; }

[JsonProperty("display_name")]
public string DisplayName { get; set; }
}
}