using Microsoft.AspNetCore.Mvc;
using NewLife;
using NewLife.Remoting;
using NewLife.Remoting.Extensions;
using NewLife.Remoting.Services;
using NewLife.Web;
using Stardust.Data;
using Stardust.Models;
using Stardust.Server.Models;
using Stardust.Server.Services;
namespace Stardust.Server.Controllers;
/// <summary>OAuth服务。向应用提供验证服务</summary>
[Route("[controller]/[action]")]
public class OAuthController(ITokenService tokenService, AppTokenService appTokenService, AppOnlineService appOnline, StarServerSetting setting) : ControllerBase
{
/// <summary>颁发访问令牌。支持密码模式与刷新令牌模式</summary>
/// <param name="model">令牌请求模型</param>
/// <returns>访问令牌</returns>
[ApiFilter]
public IToken Token([FromBody] TokenInModel model)
{
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 = appTokenService.Authorize(model.UserName, model.Password, setting.AppAutoRegister, ip, clientId);
// 更新应用信息
app.LastLogin = DateTime.Now;
app.LastIP = ip;
app.Update();
var tokenModel = tokenService.IssueToken(app.Name, clientId);
var olt = appOnline.UpdateOnline(app, clientId, ip, tokenModel.AccessToken);
app.WriteHistory("Authorize", true, model.UserName, olt?.Version, ip, clientId);
// 收敛窗口/开放模式:随令牌下发密钥,供客户端保存后自动收敛
return WithSecret(tokenModel, app);
}
// 刷新令牌
else if (model.grant_type == "refresh_token")
{
var (jwt, ex) = tokenService.DecodeToken(model.refresh_token);
// 验证应用
var app = App.FindByName(jwt?.Subject);
if (app == null || !app.Enable)
{
ex ??= new ApiException(ApiCode.Unauthorized, L.T("Invalid application [{0}]", jwt.Subject));
}
if (clientId.IsNullOrEmpty()) clientId = jwt.Id;
if (ex != null)
{
app.WriteHistory("RefreshToken", false, ex.ToString(), null, ip, clientId);
throw ex;
}
var tokenModel = tokenService.IssueToken(app.Name, clientId);
var olt = appOnline.UpdateOnline(app, clientId, ip, tokenModel.AccessToken);
//app.WriteHistory("RefreshToken", true, model.refresh_token, olt?.Version, ip, clientId);
return tokenModel;
}
else
{
throw new NotSupportedException(L.T("Unsupported grant_type={0}", model.grant_type));
}
}
catch (Exception ex)
{
var app = App.FindByName(model.UserName);
app?.WriteHistory("Authorize", false, ex.ToString(), null, ip, clientId);
throw;
}
}
/// <summary>按重注册模式放行时,随令牌下发应用密钥,供客户端保存后自动收敛。其余情况原样返回</summary>
/// <param name="token">令牌</param>
/// <param name="app">应用</param>
/// <returns>携带密钥的扩展令牌</returns>
private static IToken WithSecret(IToken token, App app)
{
if (app.ReRegister is not (ReRegisterModes.Auto or ReRegisterModes.Open)) return token;
if (app.Secret.IsNullOrEmpty()) return token;
var model = new AppTokenModel
{
AccessToken = token.AccessToken,
ExpireIn = token.ExpireIn,
RefreshToken = token.RefreshToken,
Secret = app.Secret,
};
if (token is NewLife.Web.TokenModel tm)
{
model.TokenType = tm.TokenType;
model.Scope = tm.Scope;
}
return model;
}
/// <summary>获取当前令牌对应的应用信息</summary>
/// <param name="token">访问令牌</param>
/// <returns>应用信息</returns>
[ApiFilter]
public Object UserInfo(String token)
{
var (jwt, ex) = tokenService.DecodeToken(token);
if (ex != null) throw ex;
var app = appTokenService.ValidApp(jwt?.Subject);
return new
{
app.Id,
app.Name,
app.DisplayName,
app.Category,
};
}
}
|