Skip to content

Commit 1dc31e3

Browse files
committed
added postman example of the oauth flow - getting the access token and also refreshing the token
1 parent e31b496 commit 1dc31e3

11 files changed

+313
-25
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"variables": [],
3+
"info": {
4+
"name": "WebApiBoilerplate",
5+
"_postman_id": "e7aa613e-df3e-a38b-fbe2-f4387f7fa1e0",
6+
"description": "",
7+
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
8+
},
9+
"item": [
10+
{
11+
"name": "AUTH - Receive authentication token",
12+
"request": {
13+
"url": "http://localhost:49972/auth/token",
14+
"method": "POST",
15+
"header": [
16+
{
17+
"key": "Content-Type",
18+
"value": "application/x-www-form-urlencoded",
19+
"description": ""
20+
}
21+
],
22+
"body": {
23+
"mode": "urlencoded",
24+
"urlencoded": [
25+
{
26+
"key": "username",
27+
"value": "doruc",
28+
"description": "",
29+
"type": "text"
30+
},
31+
{
32+
"key": "password",
33+
"value": "asdf3235",
34+
"description": "",
35+
"type": "text"
36+
},
37+
{
38+
"key": "grant_type",
39+
"value": "password",
40+
"description": "",
41+
"type": "text"
42+
},
43+
{
44+
"key": "client_id",
45+
"value": "F1179B6B-15A8-4250-9ED9-4C2D5EE0376B",
46+
"description": "",
47+
"type": "text"
48+
}
49+
]
50+
},
51+
"description": "Demonstrates how to receive an authentication token from the oauth layer we have set-up "
52+
},
53+
"response": []
54+
},
55+
{
56+
"name": "AUTH - Renew authentication token with refresh token",
57+
"request": {
58+
"url": "http://localhost:49972/auth/token",
59+
"method": "POST",
60+
"header": [
61+
{
62+
"key": "Content-Type",
63+
"value": "application/x-www-form-urlencoded",
64+
"description": ""
65+
}
66+
],
67+
"body": {
68+
"mode": "urlencoded",
69+
"urlencoded": [
70+
{
71+
"key": "refresh_token",
72+
"value": "2fb97c7f71a24676b08fd9fb18acad6f",
73+
"description": "",
74+
"type": "text"
75+
},
76+
{
77+
"key": "grant_type",
78+
"value": "refresh_token",
79+
"description": "",
80+
"type": "text"
81+
},
82+
{
83+
"key": "client_id",
84+
"value": "F1179B6B-15A8-4250-9ED9-4C2D5EE0376B",
85+
"description": "",
86+
"type": "text"
87+
}
88+
]
89+
},
90+
"description": "Demonstrates how to receive an authentication token from the oauth layer we have set-up "
91+
},
92+
"response": []
93+
}
94+
]
95+
}

src/App/App.Api/App.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@
331331
<DependentUpon>Global.asax</DependentUpon>
332332
</Compile>
333333
<Compile Include="Properties\AssemblyInfo.cs" />
334+
<Compile Include="Security\IRefreshTokenManager.cs" />
334335
<Compile Include="Security\JwtFormat.cs" />
335336
<Compile Include="Security\OauthProvider.cs" />
336337
<Compile Include="Security\RefreshTokenManager.cs" />

src/App/App.Api/Areas/HelpPage/HelpPageAreaRegistration.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public override string AreaName
1515

1616
public override void RegisterArea(AreaRegistrationContext context)
1717
{
18-
context.MapRoute(
19-
"HelpPage_Default",
20-
"Help/{action}/{apiId}",
21-
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
18+
// context.MapRoute(
19+
// "HelpPage_Default",
20+
// "Help/{action}/{apiId}",
21+
// new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
2222

23-
HelpPageConfig.Register(GlobalConfiguration.Configuration);
23+
//HelpPageConfig.Register(GlobalConfiguration.Configuration);
2424
}
2525
}
2626
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using App.Dto.Request;
5+
using App.Entities.Security;
6+
7+
namespace App.Api.Security
8+
{
9+
public interface IRefreshTokenManager : IDisposable
10+
{
11+
IEnumerable<Client> GetClients();
12+
IEnumerable<Client> GetAllowedClients();
13+
Client FindClient(string clientId);
14+
Task<Client> AddClientAsync(ClientBindingModel clientModel);
15+
Task<bool> RemoveClient(string id);
16+
Task<bool> AddRefreshToken(RefreshToken token);
17+
Task<bool> RemoveRefreshToken(RefreshToken existingToken);
18+
Task<bool> RemoveRefreshToken(string refreshTokenId);
19+
Task<RefreshToken> FindRefreshToken(string refreshTokenId);
20+
List<RefreshToken> GetAllRefreshTokens();
21+
}
22+
}

src/App/App.Api/Security/RefreshTokenManager.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
namespace App.Api.Security
1212
{
13-
public interface IRefreshTokenManager : IDisposable
14-
{
15-
IEnumerable<Client> GetClients();
16-
IEnumerable<Client> GetAllowedClients();
17-
Client FindClient(string clientId);
18-
Task<Client> AddClientAsync(ClientBindingModel clientModel);
19-
Task<bool> RemoveClient(string id);
20-
Task<bool> AddRefreshToken(RefreshToken token);
21-
Task<bool> RemoveRefreshToken(RefreshToken existingToken);
22-
Task<bool> RemoveRefreshToken(string refreshTokenId);
23-
Task<RefreshToken> FindRefreshToken(string refreshTokenId);
24-
List<RefreshToken> GetAllRefreshTokens();
25-
}
26-
2713
public class RefreshTokenManager : IRefreshTokenManager
2814
{
2915
private readonly DatabaseContext _context;
@@ -45,7 +31,7 @@ public IEnumerable<Client> GetAllowedClients()
4531

4632
public Client FindClient(string clientId)
4733
{
48-
var client = _context.Clients.Find(clientId);
34+
var client = _context.Clients.Find(Guid.Parse(clientId));
4935
return client;
5036
}
5137

src/App/App.Api/Security/RefreshTokenProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public async Task CreateAsync(AuthenticationTokenCreateContext context)
3535

3636
var token = new RefreshToken
3737
{
38-
Id = Guid.NewGuid(),
39-
RefreshTokenId = refreshTokenId.GetHash(),
38+
Id = refreshTokenId.GetHash(),
4039
ClientId = clientId,
4140
Subject = context.Ticket.Identity.Name,
4241
IssuedUtc = DateTime.UtcNow,
@@ -64,7 +63,7 @@ public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
6463
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
6564

6665
var hashedTokenId = context.Token.GetHash();
67-
var refreshToken = await context.OwinContext.Get<RefreshTokenManager>().FindRefreshToken(hashedTokenId);
66+
var refreshToken = await _refreshTokenManager.FindRefreshToken(hashedTokenId);
6867

6968
if (refreshToken != null)
7069
{

src/App/App.Database/App.Database.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<Compile Include="Migrations\201708080013595_RenameAspTables.Designer.cs">
9292
<DependentUpon>201708080013595_RenameAspTables.cs</DependentUpon>
9393
</Compile>
94+
<Compile Include="Migrations\201708092106290_RevertRefreshTokenId.cs" />
95+
<Compile Include="Migrations\201708092106290_RevertRefreshTokenId.Designer.cs">
96+
<DependentUpon>201708092106290_RevertRefreshTokenId.cs</DependentUpon>
97+
</Compile>
9498
<Compile Include="Security\ApplicationRoleManager.cs" />
9599
<Compile Include="Security\ApplicationUser.cs" />
96100
<Compile Include="Security\ApplicationUserManager.cs" />
@@ -146,6 +150,9 @@
146150
<EmbeddedResource Include="Migrations\201708080013595_RenameAspTables.resx">
147151
<DependentUpon>201708080013595_RenameAspTables.cs</DependentUpon>
148152
</EmbeddedResource>
153+
<EmbeddedResource Include="Migrations\201708092106290_RevertRefreshTokenId.resx">
154+
<DependentUpon>201708092106290_RevertRefreshTokenId.cs</DependentUpon>
155+
</EmbeddedResource>
149156
</ItemGroup>
150157
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
151158
</Project>

src/App/App.Database/Migrations/201708092106290_RevertRefreshTokenId.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace App.Database
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class RevertRefreshTokenId : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
DropPrimaryKey("dbo.RefreshTokens");
11+
AlterColumn("dbo.RefreshTokens", "Id", c => c.String(nullable: false, maxLength: 128));
12+
AddPrimaryKey("dbo.RefreshTokens", "Id");
13+
DropColumn("dbo.RefreshTokens", "RefreshTokenId");
14+
}
15+
16+
public override void Down()
17+
{
18+
AddColumn("dbo.RefreshTokens", "RefreshTokenId", c => c.String());
19+
DropPrimaryKey("dbo.RefreshTokens");
20+
AlterColumn("dbo.RefreshTokens", "Id", c => c.Guid(nullable: false));
21+
AddPrimaryKey("dbo.RefreshTokens", "Id");
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)