11using System . IdentityModel . Tokens . Jwt ;
2+ using System . Security . Claims ;
23using System . Text ;
34using Microsoft . AspNetCore . Authorization ;
45using Microsoft . AspNetCore . Mvc ;
56using Microsoft . EntityFrameworkCore ;
6- using Microsoft . Extensions . Logging ;
77using Microsoft . IdentityModel . Tokens ;
88using MoonCore . Exceptions ;
99using MoonCore . Extended . Abstractions ;
@@ -20,53 +20,37 @@ namespace Moonlight.ApiServer.Http.Controllers.Auth;
2020public class AuthController : Controller
2121{
2222 private readonly AppConfiguration Configuration ;
23- private readonly ILogger < AuthController > Logger ;
2423 private readonly DatabaseRepository < User > UserRepository ;
2524 private readonly IOAuth2Provider OAuth2Provider ;
2625
27- private readonly string RedirectUri ;
28- private readonly string EndpointUri ;
29-
3026 public AuthController (
3127 AppConfiguration configuration ,
32- ILogger < AuthController > logger ,
3328 DatabaseRepository < User > userRepository ,
3429 IOAuth2Provider oAuth2Provider
3530 )
3631 {
3732 UserRepository = userRepository ;
3833 OAuth2Provider = oAuth2Provider ;
3934 Configuration = configuration ;
40- Logger = logger ;
41-
42- RedirectUri = string . IsNullOrEmpty ( Configuration . Authentication . OAuth2 . AuthorizationRedirect )
43- ? Configuration . PublicUrl
44- : Configuration . Authentication . OAuth2 . AuthorizationRedirect ;
45-
46- EndpointUri = string . IsNullOrEmpty ( Configuration . Authentication . OAuth2 . AuthorizationEndpoint )
47- ? Configuration . PublicUrl + "/oauth2/authorize"
48- : Configuration . Authentication . OAuth2 . AuthorizationEndpoint ;
4935 }
5036
5137 [ AllowAnonymous ]
5238 [ HttpGet ( "start" ) ]
53- public Task < LoginStartResponse > Start ( )
39+ public async Task < LoginStartResponse > Start ( )
5440 {
55- var response = new LoginStartResponse ( )
41+ var url = await OAuth2Provider . Start ( ) ;
42+
43+ return new LoginStartResponse ( )
5644 {
57- ClientId = Configuration . Authentication . OAuth2 . ClientId ,
58- RedirectUri = RedirectUri ,
59- Endpoint = EndpointUri
45+ Url = url
6046 } ;
61-
62- return Task . FromResult ( response ) ;
6347 }
6448
6549 [ AllowAnonymous ]
6650 [ HttpPost ( "complete" ) ]
6751 public async Task < LoginCompleteResponse > Complete ( [ FromBody ] LoginCompleteRequest request )
6852 {
69- var user = await OAuth2Provider . Sync ( request . Code ) ;
53+ var user = await OAuth2Provider . Complete ( request . Code ) ;
7054
7155 if ( user == null )
7256 throw new HttpApiException ( "Unable to load user data" , 500 ) ;
@@ -113,8 +97,8 @@ public async Task<LoginCompleteResponse> Complete([FromBody] LoginCompleteReques
11397 [ HttpGet ( "check" ) ]
11498 public async Task < CheckResponse > Check ( )
11599 {
116- var userIdClaim = User . Claims . First ( x => x . Type == "userId" ) ;
117- var userId = int . Parse ( userIdClaim . Value ) ;
100+ var userIdStr = User . FindFirstValue ( "userId" ) ! ;
101+ var userId = int . Parse ( userIdStr ) ;
118102 var user = await UserRepository . Get ( ) . FirstAsync ( x => x . Id == userId ) ;
119103
120104 return new ( )
0 commit comments