Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Commit

Permalink
Merge pull request #1 from psibernetic/finalization
Browse files Browse the repository at this point in the history
Finalization
  • Loading branch information
Ovan Crone authored Aug 30, 2016
2 parents 321dcb5 + d8a5025 commit 61b965c
Show file tree
Hide file tree
Showing 17 changed files with 4,610 additions and 266 deletions.
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"program": "${workspaceRoot}/src/JwtAndCookie.Example/bin/Debug/netcoreapp1.0/JwtAndCookie.Example.dll",
"args": [],
"cwd": "${workspaceRoot}",
"externalConsole": false,
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command.pickProcess}"
}
]
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"src/JwtAndCookie"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# OwinJwtAndCookie
An owin middleware for JWT token parsing from Authorization header and cookies with an owin signin delegate with optional cookie writing capabilities.
# JwtAndCookie
An owin middleware for JOSE RSA JWE & JWT token parsing from Authorization header and cookies with an owin signin delegate with optional cookie writing capabilities.

## OWIN configuration

Add the following to your OWIN startup or IAppBuilder, feeding your values from your own configuration scheme.
Add the following to your ASPNETCORE startup, feeding your values from your own configuration scheme.

```csharp
app.UseJwtAndCookieMiddleware(new JwtAndCookieMiddlewareOptions
app.UseOwin(owin =>
{
PassPhrase = configuration.HmacPassphrase,
CookieName = configuration.CookieName,
CookiePath = configuration.CookiePath,
CookieHttpOnly = true,
TokenLifeSpan = TimeSpan.FromMinutes(configuration.TokenLifeSpan),
ClaimsPrincipalResourceName = configuration.ClaimsPrincipalResourceName,
CreatePrincipal = CreatePrincipal
owin((next) =>
new JwtAndCookieMiddleware(next, new Options
{
Certificate = new X509Certificate2("C:\\jwtmiddleware.pfx", "test"),
CookieName = "jwt",
CookiePath = "/",
CookieHttpOnly = true,
TokenLifeSpan = TimeSpan.FromMinutes(30),
ClaimsPrincipalResourceName = "principal",
CreatePrincipal = (payload) => new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("meh"))) //Example func
}).Invoke);
});
```

Expand Down
3 changes: 3 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"projects": [ "src", "test" ]
}
6 changes: 0 additions & 6 deletions src/JwtAndCookie/App.config

This file was deleted.

30 changes: 18 additions & 12 deletions src/JwtAndCookie/JwtAndCookieMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using JWT;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;

namespace OwinJwtAndCookie
{
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using AppFunc = Func<IDictionary<string, object>, Task>;

public class JwtAndCookieMiddleware
{
private readonly AppFunc _next;
private readonly JwtAndCookieMiddlewareOptions _options;
private readonly Options _options;

public JwtAndCookieMiddleware(AppFunc next, JwtAndCookieMiddlewareOptions options)
public JwtAndCookieMiddleware(AppFunc next, Options options)
{
_next = next;
_options = options;

Jose.JWT.JsonMapper = new Jose.NewtonsoftMapper();
}

public Task Invoke(IDictionary<string, object> environment)
Expand Down Expand Up @@ -61,18 +65,19 @@ public Task Invoke(IDictionary<string, object> environment)
return _next(environment);
}

private void DefineJwtGenerator(IDictionary<string, object> environment, JwtAndCookieMiddlewareOptions options)
private void DefineJwtGenerator(IDictionary<string, object> environment, Options options)
{
environment["jwtandcookie.signin"] = new Func<Func<Guid, IDictionary<string, object>>, bool, string>(
(claimBuilder, buildCookie) =>
{
var jti = Guid.NewGuid();
var jwt = JsonWebToken.Encode(new Dictionary<string, object>
var jwt = Jose.JWT.Encode(new Dictionary<string, object>
{
{ "jti", jti.ToString() },
{ "exp", BuildExpHeader(options) }
}.Union(claimBuilder(jti)).ToDictionary(p => p.Key, p => p.Value), _options.PassPhrase,
JwtHashAlgorithm.HS256);
}.Union(claimBuilder(jti)).ToDictionary(p => p.Key, p => p.Value),
_options.Certificate.GetRSAPublicKey() as RSACng,
Jose.JweAlgorithm.RSA_OAEP, Jose.JweEncryption.A256GCM);

if (!buildCookie) return jwt;

Expand All @@ -84,7 +89,7 @@ private void DefineJwtGenerator(IDictionary<string, object> environment, JwtAndC
});
}

private static string BuildExpHeader(JwtAndCookieMiddlewareOptions options)
private static string BuildExpHeader(Options options)
{
var timePeriod = DateTime.UtcNow
- new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
Expand All @@ -96,14 +101,15 @@ private static string BuildExpHeader(JwtAndCookieMiddlewareOptions options)

private IDictionary<string, object> DecodeAndValidateToken(string token)
{

IDictionary<string, object> payload = null;

try
{
payload = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, _options.PassPhrase);
payload = Jose.JWT.Decode<IDictionary<string, object>>(token,
_options.Certificate.GetRSAPrivateKey() as RSACng,
Jose.JweAlgorithm.RSA_OAEP, Jose.JweEncryption.A256GCM);
}
catch (SignatureVerificationException) //No meaningful valid payload, return null.
catch (Jose.JoseException) //No meaningful valid payload, return null.
{
return payload;
}
Expand Down
19 changes: 0 additions & 19 deletions src/JwtAndCookie/JwtAndCookieMiddlewareExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;

namespace OwinJwtAndCookie
{
public class JwtAndCookieMiddlewareOptions
public class Options
{
public JwtAndCookieMiddlewareOptions()
public Options()
{
CookieHttpOnly = true;
}

public string PassPhrase { get; set; }


public string CookieName { get; set; }

public string CookiePath { get; set; }

public bool CookieHttpOnly { get; set; }

public X509Certificate2 Certificate { get; set; }

public string ClaimsPrincipalResourceName { get; set; }

public TimeSpan TokenLifeSpan { get; set; } = TimeSpan.FromMinutes(30);
Expand Down
74 changes: 0 additions & 74 deletions src/JwtAndCookie/OwinJwtAndCookie.csproj

This file was deleted.

52 changes: 0 additions & 52 deletions src/JwtAndCookie/OwinJwtAndCookie.csproj.GhostDoc.xml

This file was deleted.

17 changes: 0 additions & 17 deletions src/JwtAndCookie/OwinJwtAndCookie.nuspec

This file was deleted.

4 changes: 2 additions & 2 deletions src/JwtAndCookie/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
5 changes: 0 additions & 5 deletions src/JwtAndCookie/packages.config

This file was deleted.

Loading

0 comments on commit 61b965c

Please sign in to comment.