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

Update sample.http to support user creation then token generation from the new users #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions TodoBasicWithAuth/AuthApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public AuthApi(JwtSettings jwtSettings)

public async Task CreateUser(UserManager<TodoUser> userManager, HttpContext context)
{
var loginInfo = await JsonSerializer.DeserializeAsync<LoginInfo>(context.Request.Body, _options);
var loginInfo = await JsonSerializer.DeserializeAsync<CreateUser>(context.Request.Body, _options);

var result = await userManager.CreateAsync(new TodoUser { UserName = loginInfo.UserName }, loginInfo.Password);
var result = await userManager.CreateAsync(new TodoUser { UserName = loginInfo.UserName, IsAdmin = loginInfo.IsAdmin }, loginInfo.Password);

if (result.Succeeded)
{
Expand All @@ -55,13 +55,15 @@ public async Task GenerateTokenAsync(UserManager<TodoUser> userManager, HttpCont
}

var claims = new List<Claim>();

claims.Add(new Claim("can_view", "true"));

if (user.IsAdmin)
{
claims.Add(new Claim("can_delete", "true"));
claims.Add(new Claim("can_view", "true"));
}


stevenknox marked this conversation as resolved.
Show resolved Hide resolved

var key = new SymmetricSecurityKey(_jwtSettings.Key);
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
Expand Down
19 changes: 19 additions & 0 deletions TodoBasicWithAuth/CreateUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Todos
{
public class CreateUser
{
[Required]
public string UserName { get; set; }

[Required]
public string Password { get; set; }

public bool IsAdmin { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with this. I think the IsAdmin logic should stay server side. Even though this is a demo app 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no worries, I could revert the CreateUser back to LoginInfo so users can only be created as a regular user via the API then seed the Admin user on startup? Or is that overkill for this project?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or the first user is admin with a comment describing that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the insecure user creation back to the original implementation and seeded admin on startup. sample.http now works with the new approach

}
}
54 changes: 39 additions & 15 deletions TodoBasicWithAuth/sample.http
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
@token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjYW5fZGVsZXRlIjoidHJ1ZSIsImNhbl92aWV3IjoidHJ1ZSIsImV4cCI6MTU4MTU2NTU2MiwiaXNzIjoiZGVmYXVsdGlzc3VlciIsImF1ZCI6ImRlZmF1bHRhdWRpZW5jZSJ9.XJyk0vIcuy1x4Kdk2O6N9I3Ibg3Qs4dOlFiiQOle2pk
@todo_id = 1
###

// Needs https://marketplace.visualstudio.com/items?itemName=humao.rest-client
// Get all todos (no authentication)
GET http://localhost:5000/api/todos

###

//Create regular user
POST http://localhost:5000/api/auth
Content-Type: application/json

{
"username" : "user",
"password" : "Hunter2!"
}

###

// Authenticate as Admin
POST http://localhost:5000/api/auth/token
// Create admin user
POST http://localhost:5000/api/auth
Content-Type: application/json

{
"username" : "admin",
"password" : "123456"
"password" : "Pass123456!",
"isAdmin" : true
}

###

// Authenticate as regular user
# @name LoginRegularUser
POST http://localhost:5000/api/auth/token
Content-Type: application/json

{
"username" : "user",
"password" : "hunter2"
"password" : "Hunter2!"
}

@token = {{LoginRegularUser.response.body.token}}
###

// New Todo
POST http://localhost:5000/api/todos
Authorization: Bearer {{token}}
Content-Type: application/json

{
"Name" : "Write unit tests.",
"IsComplete" : false
}

###
Expand All @@ -43,21 +65,23 @@ Authorization: Bearer {{token}}
GET http://localhost:5000/api/todos/{{todo_id}}
Authorization: Bearer {{token}}


###

// New Todo
POST http://localhost:5000/api/todos
Authorization: Bearer {{token}}
// Authenticate as an admin user
# @name LoginAdminUser
POST http://localhost:5000/api/auth/token
Content-Type: application/json

{
"Name" : "Write unit tests.",
"IsComplete" : false
"username" : "admin",
"password" : "Pass123456!"
}

###

// Delete Todo
// Delete Todo. must be authenticated as admim

@admintoken = {{LoginAdminUser.response.body.token}}

DELETE http://localhost:5000/api/todos/{{todo_id}}
Authorization: Bearer {{token}}
Authorization: Bearer {{admintoken}}