From a9d25a149262f9ddb6b7e65033270a5f8322d420 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 11 Dec 2024 22:12:53 +0000 Subject: [PATCH] WIP OAuth token grant --- Astral.ApiServer/Astral.ApiServer.csproj | 4 -- Astral.ApiServer/Constants/OAuthGrantTypes.cs | 16 +++++++ .../Controllers/OAuthController.cs | 29 +++++++++++++ .../Models/TokenGrantRequestModel.cs | 43 +++++++++++++++++++ .../Models/TokenGrantResponseModel.cs | 37 ++++++++++++++++ Astral.Services/Services/UserService.cs | 8 ++-- 6 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 Astral.ApiServer/Constants/OAuthGrantTypes.cs create mode 100644 Astral.ApiServer/Controllers/OAuthController.cs create mode 100644 Astral.ApiServer/Models/TokenGrantRequestModel.cs create mode 100644 Astral.ApiServer/Models/TokenGrantResponseModel.cs diff --git a/Astral.ApiServer/Astral.ApiServer.csproj b/Astral.ApiServer/Astral.ApiServer.csproj index 7f8c717..4b02de7 100644 --- a/Astral.ApiServer/Astral.ApiServer.csproj +++ b/Astral.ApiServer/Astral.ApiServer.csproj @@ -21,10 +21,6 @@ - - - - diff --git a/Astral.ApiServer/Constants/OAuthGrantTypes.cs b/Astral.ApiServer/Constants/OAuthGrantTypes.cs new file mode 100644 index 0000000..2e7c827 --- /dev/null +++ b/Astral.ApiServer/Constants/OAuthGrantTypes.cs @@ -0,0 +1,16 @@ +// +// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License. +// + +namespace Astral.ApiServer.Constants; + +/// +/// Available grant types for auth token requests. +/// +public static class OAuthGrantTypes +{ + /// + /// Password grant type. + /// + public const string Password = "password"; +} diff --git a/Astral.ApiServer/Controllers/OAuthController.cs b/Astral.ApiServer/Controllers/OAuthController.cs new file mode 100644 index 0000000..8c701e3 --- /dev/null +++ b/Astral.ApiServer/Controllers/OAuthController.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License. +// + +using Astral.ApiServer.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Astral.ApiServer.Controllers; + +/// +/// OAuth authentication controller. +/// +[Produces("application/json")] +[Consumes("application/x-www-form-urlencoded")] +[Route("oauth")] +public class OAuthController : ControllerBase +{ + /// + /// Grant token request. + /// + /// Instance of . + [HttpPost("token")] + [AllowAnonymous] + public Task GrantToken([FromForm] TokenGrantRequestModel tokenGrantRequest) + { + throw new NotImplementedException(); + } +} diff --git a/Astral.ApiServer/Models/TokenGrantRequestModel.cs b/Astral.ApiServer/Models/TokenGrantRequestModel.cs new file mode 100644 index 0000000..9c9104d --- /dev/null +++ b/Astral.ApiServer/Models/TokenGrantRequestModel.cs @@ -0,0 +1,43 @@ +// +// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License. +// + +using Microsoft.AspNetCore.Mvc; + +namespace Astral.ApiServer.Models; + +/// +/// Oauth token grant request. +/// +public class TokenGrantRequestModel +{ + /// + /// The grant type of the request. + /// + [FromForm(Name = "grant_type")] + public string GrantType { get; set; } + + /// + /// Refresh token. + /// + [FromForm(Name = "refresh_token")] + public string RefreshToken { get; set; } + + /// + /// Username. + /// + [FromForm(Name = "username")] + public string Username { get; set; } + + /// + /// Password. + /// + [FromForm(Name = "password")] + public string Password { get; set; } + + /// + /// Scope. + /// + [FromForm(Name = "scope")] + public string Scope { get; set; } +} diff --git a/Astral.ApiServer/Models/TokenGrantResponseModel.cs b/Astral.ApiServer/Models/TokenGrantResponseModel.cs new file mode 100644 index 0000000..b36bf1f --- /dev/null +++ b/Astral.ApiServer/Models/TokenGrantResponseModel.cs @@ -0,0 +1,37 @@ +// +// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License. +// + +using System.Text.Json.Serialization; + +namespace Astral.ApiServer.Models; + +/// +/// OAuth Grant Request Response. +/// +public class TokenGrantResponseModel +{ + /// + /// The granted access token. + /// + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + + /// + /// The granted refresh token. + /// + [JsonPropertyName("refresh_token")] + public string RefreshToken { get; set; } + + /// + /// When it expires (ticks). + /// + [JsonPropertyName("expires_in")] + public long ExpiresIn { get; set; } + + /// + /// Granted token type. + /// + [JsonPropertyName("token_type")] + public string TokenType { get; set; } +} diff --git a/Astral.Services/Services/UserService.cs b/Astral.Services/Services/UserService.cs index 1b1d747..6828bd1 100644 --- a/Astral.Services/Services/UserService.cs +++ b/Astral.Services/Services/UserService.cs @@ -38,13 +38,13 @@ public class UserService : IUserService /// /// Initializes a new instance of the class. /// - /// Instance of . - /// Instance of . - /// Instance of . - /// Instance of . /// Instance of . + /// Instance of . + /// Instance of . /// Instance of . + /// Instance of . /// Instance of . + /// Instance of . /// Instance of . /// Instance of . public UserService(