├── .gitattributes
├── .gitignore
├── README.md
└── src
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── Nancy.Demo.StatelessAuth.sln
└── Nancy.Demo.StatelessAuth
├── AuthenticationBootstrapper.cs
├── DemoUserIdentity.cs
├── IUserApiMapper.cs
├── MainModule.cs
├── Nancy.Demo.StatelessAuth.csproj
├── Program.cs
├── UserApiMapper.cs
└── packages.config
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2 | [Bb]in/
3 | [Oo]bj/
4 |
5 | # mstest test results
6 | TestResults
7 |
8 | ## Ignore Visual Studio temporary files, build results, and
9 | ## files generated by popular Visual Studio add-ons.
10 |
11 | # User-specific files
12 | *.suo
13 | *.user
14 | *.sln.docstates
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Rr]elease/
19 | x64/
20 | *_i.c
21 | *_p.c
22 | *.ilk
23 | *.meta
24 | *.obj
25 | *.pch
26 | *.pdb
27 | *.pgc
28 | *.pgd
29 | *.rsp
30 | *.sbr
31 | *.tlb
32 | *.tli
33 | *.tlh
34 | *.tmp
35 | *.log
36 | *.vspscc
37 | *.vssscc
38 | .builds
39 |
40 | # Visual C++ cache files
41 | ipch/
42 | *.aps
43 | *.ncb
44 | *.opensdf
45 | *.sdf
46 |
47 | # Visual Studio profiler
48 | *.psess
49 | *.vsp
50 | *.vspx
51 |
52 | # Guidance Automation Toolkit
53 | *.gpState
54 |
55 | # ReSharper is a .NET coding add-in
56 | _ReSharper*
57 |
58 | # NCrunch
59 | *.ncrunch*
60 | .*crunch*.local.xml
61 |
62 | # Installshield output folder
63 | [Ee]xpress
64 |
65 | # DocProject is a documentation generator add-in
66 | DocProject/buildhelp/
67 | DocProject/Help/*.HxT
68 | DocProject/Help/*.HxC
69 | DocProject/Help/*.hhc
70 | DocProject/Help/*.hhk
71 | DocProject/Help/*.hhp
72 | DocProject/Help/Html2
73 | DocProject/Help/html
74 |
75 | # Click-Once directory
76 | publish
77 |
78 | # Publish Web Output
79 | *.Publish.xml
80 |
81 | # NuGet Packages Directory
82 | packages
83 |
84 | # Windows Azure Build Output
85 | csx
86 | *.build.csdef
87 |
88 | # Windows Store app package directory
89 | AppPackages/
90 |
91 | # Others
92 | [Bb]in
93 | [Oo]bj
94 | sql
95 | TestResults
96 | [Tt]est[Rr]esult*
97 | *.Cache
98 | ClientBin
99 | [Ss]tyle[Cc]op.*
100 | ~$*
101 | *.dbmdl
102 | Generated_Code #added for RIA/Silverlight projects
103 |
104 | # Backup & report files from converting an old project file to a newer
105 | # Visual Studio version. Backup files are not needed, because we have git ;-)
106 | _UpgradeReport_Files/
107 | Backup*/
108 | UpgradeLog*.XML
109 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Nancy.Demo.StatelessAuth
2 |
3 | This is a demo for showing how you can implement stateless authentication in you Nancy applications.
4 |
5 | When a request is made to the application it checks for a token in a authorization header.
6 |
7 | This is then passed to a user validator class which returns a IUserIdentity if the user is deemed valid.
8 |
9 | If the user is not validated then the request is not allowed to continue
--------------------------------------------------------------------------------
/src/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jchannon/Nancy.Demo.StatelessAuth/b8c9dc84f60185146124a77a15d969030de2a073/src/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/src/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
25 |
26 |
27 |
28 |
29 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
30 | $([System.IO.Path]::Combine($(ProjectDir), "packages.config"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 | packages.config
37 |
38 |
39 |
40 |
41 | $(NuGetToolsPath)\nuget.exe
42 | @(PackageSource)
43 |
44 | "$(NuGetExePath)"
45 | mono --runtime=v4.0.30319 $(NuGetExePath)
46 |
47 | $(TargetDir.Trim('\\'))
48 |
49 | -RequireConsent
50 |
51 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -solutionDir "$(SolutionDir) "
52 | $(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols
53 |
54 |
55 |
56 | RestorePackages;
57 | $(ResolveReferencesDependsOn);
58 |
59 |
60 |
61 |
62 | $(BuildDependsOn);
63 | BuildPackage;
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
86 |
87 |
90 |
91 |
92 |
93 |
95 |
96 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2012
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.StatelessAuth", "Nancy.Demo.StatelessAuth\Nancy.Demo.StatelessAuth.csproj", "{0E050905-223E-42FA-95D2-B7877F9E99EF}"
5 | EndProject
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{7409D3E4-FD3C-4DF8-95FC-BDC6C4A8FC79}"
7 | ProjectSection(SolutionItems) = preProject
8 | .nuget\NuGet.Config = .nuget\NuGet.Config
9 | .nuget\NuGet.exe = .nuget\NuGet.exe
10 | .nuget\NuGet.targets = .nuget\NuGet.targets
11 | EndProjectSection
12 | EndProject
13 | Global
14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
15 | Debug|Any CPU = Debug|Any CPU
16 | Release|Any CPU = Release|Any CPU
17 | EndGlobalSection
18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
19 | {0E050905-223E-42FA-95D2-B7877F9E99EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20 | {0E050905-223E-42FA-95D2-B7877F9E99EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
21 | {0E050905-223E-42FA-95D2-B7877F9E99EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
22 | {0E050905-223E-42FA-95D2-B7877F9E99EF}.Release|Any CPU.Build.0 = Release|Any CPU
23 | EndGlobalSection
24 | GlobalSection(SolutionProperties) = preSolution
25 | HideSolutionNode = FALSE
26 | EndGlobalSection
27 | EndGlobal
28 |
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/AuthenticationBootstrapper.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using System;
4 | using Nancy;
5 | using Nancy.Authentication.Stateless;
6 | using Nancy.Bootstrapper;
7 | using Nancy.TinyIoc;
8 |
9 | public class AuthenticationBootstrapper : DefaultNancyBootstrapper
10 | {
11 | protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
12 | {
13 | base.ApplicationStartup(container, pipelines);
14 |
15 | // At request startup we modify the request pipelines to
16 | // include stateless authentication
17 | //
18 | // Configuring stateless authentication is simple. Just use the
19 | // NancyContext to get the apiKey. Then, use the apiKey to get
20 | // your user's identity.
21 | var configuration =
22 | new StatelessAuthenticationConfiguration(nancyContext =>
23 | {
24 | const string key = "Bearer ";
25 | string accessToken = null;
26 |
27 | if (nancyContext.Request.Headers.Authorization.StartsWith(key))
28 | {
29 | accessToken = nancyContext.Request.Headers.Authorization.Substring(key.Length);
30 | }
31 |
32 | if (string.IsNullOrWhiteSpace(accessToken))
33 | return null;
34 |
35 | var userValidator = container.Resolve();
36 |
37 | return userValidator.GetUserFromAccessToken(accessToken);
38 | });
39 |
40 | StatelessAuthentication.Enable(pipelines, configuration);
41 |
42 | //Make every request SSL based
43 | //pipelines.BeforeRequest += ctx =>
44 | //{
45 | // return (!ctx.Request.Url.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) ?
46 | // (Response)HttpStatusCode.Unauthorized :
47 | // null;
48 | //};
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/DemoUserIdentity.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using System.Collections.Generic;
4 | using Nancy.Security;
5 |
6 | public class DemoUserIdentity : IUserIdentity
7 | {
8 | public string UserName { get; set; }
9 |
10 | public IEnumerable Claims { get; set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/IUserApiMapper.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using Nancy.Security;
4 |
5 | public interface IUserApiMapper
6 | {
7 | IUserIdentity GetUserFromAccessToken(string accessToken);
8 | }
9 | }
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/MainModule.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using Nancy;
4 | using Nancy.Security;
5 |
6 | public class MainModule : NancyModule
7 | {
8 | public MainModule()
9 | {
10 | this.RequiresAuthentication();
11 |
12 | Get["/GetData"] = parameters =>
13 | {
14 | var data = new { Name = "John", LastName = "Smith" };
15 |
16 | return Negotiate.WithModel(data);
17 | };
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/Nancy.Demo.StatelessAuth.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {0E050905-223E-42FA-95D2-B7877F9E99EF}
8 | Exe
9 | Properties
10 | Nance.Demo.StatelessAuth
11 | Nance.Demo.StatelessAuth
12 | v4.0
13 | 512
14 | ..\
15 | true
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 | ..\packages\Nancy.0.13.0\lib\net40\Nancy.dll
39 |
40 |
41 | ..\packages\Nancy.Authentication.Stateless.0.13.0\lib\net40\Nancy.Authentication.Stateless.dll
42 |
43 |
44 | ..\packages\Nancy.Hosting.Self.0.13.0\lib\net40\Nancy.Hosting.Self.dll
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
77 |
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/Program.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 |
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var nancyHost = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:1234"));
13 | nancyHost.Start();
14 | Console.WriteLine("NancyFX API running");
15 | Console.ReadLine();
16 | nancyHost.Stop();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/UserApiMapper.cs:
--------------------------------------------------------------------------------
1 | namespace Nancy.Demo.StatelessAuth
2 | {
3 | using Nancy.Security;
4 |
5 | public class UserApiMapper : IUserApiMapper
6 | {
7 | public IUserIdentity GetUserFromAccessToken(string accessToken)
8 | {
9 | if (accessToken == "fred")
10 | return new DemoUserIdentity { UserName = "Fred" };
11 |
12 | return null;
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/Nancy.Demo.StatelessAuth/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------