完成密钥验证和令牌颁发

This commit is contained in:
大石头 2024-01-18 11:32:30 +08:00
parent c6949c787f
commit 5bbdd6b488
4 changed files with 92 additions and 14 deletions

View File

@ -1,19 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AntJob.Data;
using AntJob.Data.Entity;
using AntJob.Models;
using NewLife;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Net;
using NewLife.Security;
using AntJob.Models;
using NewLife.Data;
using NewLife.Remoting;
using NewLife.Security;
using NewLife.Web;
using System.Reflection;
using System.Xml.Linq;
namespace AntJob.Server.Services;

View File

@ -5,6 +5,7 @@ using AntJob.Models;
using AntJob.Server;
using AntJob.Server.Services;
using AntJob.Web.Common;
using AntJob.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
@ -15,6 +16,7 @@ using NewLife.Cube;
using NewLife.Log;
using NewLife.Remoting;
using NewLife.Serialization;
using NewLife.Web;
using IActionFilter = Microsoft.AspNetCore.Mvc.Filters.IActionFilter;
namespace AntJob.Web.Controllers;
@ -23,8 +25,8 @@ namespace AntJob.Web.Controllers;
[Route("[controller]")]
public class AntJobController : ControllerBase, IActionFilter
{
/// <summary>令牌</summary>
public String Token { get; private set; }
///// <summary>令牌</summary>
//public String Token { get; private set; }
/// <summary>用户主机</summary>
public String UserHost => HttpContext.GetUserHost();
@ -48,7 +50,7 @@ public class AntJobController : ControllerBase, IActionFilter
{
_args = context.ActionArguments;
var token = Token = ApiFilterAttribute.GetToken(context.HttpContext);
var token = ApiFilterAttribute.GetToken(context.HttpContext);
try
{
@ -108,6 +110,60 @@ public class AntJobController : ControllerBase, IActionFilter
return rs;
}
[ApiFilter]
public TokenModel Token([FromBody] TokenInModel model)
{
var set = _setting;
if (model.grant_type.IsNullOrEmpty()) model.grant_type = "password";
var ip = HttpContext.GetUserHost();
var clientId = model.ClientId;
try
{
// 密码模式
if (model.grant_type == "password")
{
var (app, rs) = _appService.Login(new LoginModel { User = model.UserName, Pass = model.Password }, ip);
var tokenModel = _appService.IssueToken(app.Name, set);
_appService.WriteHistory(app, "Authorize", true, model.ToJson(), ip);
return tokenModel;
}
// 刷新令牌
else if (model.grant_type == "refresh_token")
{
var (app, ex) = _appService.DecodeToken(model.refresh_token, set.TokenSecret);
if (ex != null)
{
_appService.WriteHistory(app, "RefreshToken", false, ex.ToString(), ip);
throw ex;
}
var tokenModel = _appService.IssueToken(app.Name, set);
//app.WriteHistory("RefreshToken", true, model.refresh_token, olt?.Version, ip, clientId);
return tokenModel;
}
else
{
throw new NotSupportedException($"未支持 grant_type={model.grant_type}");
}
}
catch (Exception ex)
{
var app = App.FindByName(model.UserName);
_appService.WriteHistory(app, "Authorize", false, ex.ToString(), ip);
throw;
}
}
/// <summary>获取当前应用的所有在线实例</summary>
/// <returns></returns>
[HttpGet(nameof(GetPeers))]

View File

@ -0,0 +1,20 @@
namespace AntJob.Web.Models;
/// <summary>访问令牌输入参数</summary>
public class TokenInModel
{
/// <summary>授权类型</summary>
public String grant_type { get; set; }
/// <summary>用户名</summary>
public String UserName { get; set; }
/// <summary>密码</summary>
public String Password { get; set; }
/// <summary>客户端唯一标识。一般是IP@进程</summary>
public String ClientId { get; set; }
/// <summary>刷新令牌</summary>
public String refresh_token { get; set; }
}

View File

@ -2,6 +2,7 @@
using AntJob.Handlers;
using AntJob.Models;
using NewLife;
using NewLife.Http;
using NewLife.Log;
using NewLife.Remoting;
using NewLife.Threading;
@ -53,14 +54,21 @@ public class HttpJobProvider : JobProvider
var svr = Server?.Split(",").Where(e => e.StartsWithIgnoreCase("http://", "https://")).Join(",");
// 使用配置中心账号
var ant = new ApiHttpClient(svr)
var client = new ApiHttpClient(svr)
{
Tracer = Tracer,
};
client.Filter = new TokenHttpFilter
{
Action = "/AntJob/Login",
UserName = AppId,
Password = Secret,
};
// 断开前一个连接
Client.TryDispose();
Client = ant;
Client = client;
}
/// <summary>开始</summary>