1
1
using System ;
2
- using System . Linq ;
3
2
using System . Net ;
4
3
using System . Net . Http ;
5
4
using System . Net . Http . Json ;
5
+ using System . Security . Cryptography . X509Certificates ;
6
6
using System . Text ;
7
7
using System . Text . Json . Nodes ;
8
8
using System . Text . Json . Serialization ;
9
- using System . Text . RegularExpressions ;
10
9
using System . Threading . Tasks ;
11
10
using Jellyfin . Plugin . Authelia_Auth . Config ;
12
11
using MediaBrowser . Controller . Authentication ;
@@ -46,8 +45,6 @@ public class UserInfoResponse
46
45
/// </summary>
47
46
public class Authenticator
48
47
{
49
- private Regex cookieRe = new Regex ( "authelia_session=([^;]+);" ) ;
50
-
51
48
/// <summary>
52
49
/// Authenticate user.
53
50
/// </summary>
@@ -59,59 +56,65 @@ public class Authenticator
59
56
public async Task < ProviderAuthenticationResult > Authenticate ( PluginConfiguration config , string username , string password )
60
57
{
61
58
var cookieContainer = new CookieContainer ( ) ;
62
- using ( var handler = new HttpClientHandler ( ) { CookieContainer = cookieContainer } )
63
- using ( var client = new HttpClient ( handler ) { BaseAddress = new Uri ( config . AutheliaServer ) } )
59
+ using var handler = new HttpClientHandler ( )
64
60
{
65
- var jsonBody = new JsonObject ( ) ;
66
- jsonBody . Add ( "username" , username ) ;
67
- jsonBody . Add ( "password" , password ) ;
68
- jsonBody . Add ( "targetURL" , config . JellyfinUrl ) ;
69
- jsonBody . Add ( "requestMethod" , "GET" ) ;
70
- jsonBody . Add ( "keepMeLoggedIn" , true ) ;
71
-
72
- var session = string . Empty ;
73
-
74
- using ( var content = new StringContent ( jsonBody . ToString ( ) , Encoding . UTF8 , "application/json" ) )
61
+ CookieContainer = cookieContainer ,
62
+ ServerCertificateCustomValidationCallback = ( message , cert , chain , _ ) =>
75
63
{
76
- var response = await client . PostAsync ( "/api/firstfactor" , content ) ;
77
-
78
- if ( ! response . IsSuccessStatusCode )
64
+ if ( ! string . IsNullOrWhiteSpace ( config . AutheliaRootCa ) )
79
65
{
80
- throw new AuthenticationException ( "Invalid username or password." ) ;
66
+ chain . ChainPolicy . TrustMode = X509ChainTrustMode . CustomRootTrust ;
67
+ chain . ChainPolicy . CustomTrustStore . ImportFromPem ( config . AutheliaRootCa ) ;
81
68
}
82
69
83
- var setCookie = response . Headers . GetValues ( "Set-Cookie" ) . FirstOrDefault ( string . Empty ) ;
84
- session = cookieRe . Match ( setCookie ) . Groups [ 1 ] . Value ;
70
+ return chain . Build ( cert ) ;
85
71
}
72
+ } ;
73
+ using var client = new HttpClient ( handler ) { BaseAddress = new Uri ( config . AutheliaServer ) } ;
86
74
87
- // Allow using internal authelia url instead of proxied
88
- cookieContainer . Add ( new Uri ( config . AutheliaServer ) , new Cookie ( "authelia_session" , session ) ) ;
75
+ var jsonBody = new JsonObject
76
+ {
77
+ { "username" , username } ,
78
+ { "password" , password } ,
79
+ { "targetURL" , config . JellyfinUrl } ,
80
+ { "requestMethod" , "GET" } ,
81
+ { "keepMeLoggedIn" , true }
82
+ } ;
89
83
90
- using ( var request = new HttpRequestMessage ( HttpMethod . Get , "/api/verify" ) )
84
+ using ( var content = new StringContent ( jsonBody . ToString ( ) , Encoding . UTF8 , "application/json" ) )
85
+ {
86
+ var response = await client . PostAsync ( "/api/firstfactor" , content ) ;
87
+
88
+ if ( ! response . IsSuccessStatusCode )
91
89
{
92
- request . Headers . Add ( "X-Original-Url" , config . JellyfinUrl ) ;
93
- request . Headers . Add ( "X-Forwarded-Method" , "GET" ) ;
94
- var accessResponse = await client . SendAsync ( request ) ;
95
- if ( ! accessResponse . IsSuccessStatusCode )
96
- {
97
- throw new AuthenticationException ( "User doesn't have access to this service." ) ;
98
- }
90
+ throw new AuthenticationException ( "Invalid username or password." ) ;
99
91
}
92
+ }
100
93
101
- try
94
+ using ( var request = new HttpRequestMessage ( HttpMethod . Get , "/api/verify" ) )
95
+ {
96
+ request . Headers . Add ( "X-Original-Url" , config . JellyfinUrl ) ;
97
+ request . Headers . Add ( "X-Forwarded-Method" , "GET" ) ;
98
+ var accessResponse = await client . SendAsync ( request ) ;
99
+ if ( ! accessResponse . IsSuccessStatusCode )
102
100
{
103
- var userInfoResponse = await client . GetFromJsonAsync < UserInfoResponse > ( "/api/user/info" ) ;
104
-
105
- return new ProviderAuthenticationResult
106
- {
107
- Username = username ,
108
- DisplayName = userInfoResponse . Data . DisplayName ,
109
- } ;
101
+ throw new AuthenticationException ( "User doesn't have access to this service." ) ;
110
102
}
111
- catch
103
+ }
104
+
105
+ try
106
+ {
107
+ var userInfoResponse = await client . GetFromJsonAsync < UserInfoResponse > ( "/api/user/info" ) ;
108
+
109
+ return new ProviderAuthenticationResult
112
110
{
113
- throw new AuthenticationException ( "Invalid username or password." ) ;
114
- }
111
+ Username = username ,
112
+ DisplayName = userInfoResponse . Data . DisplayName ,
113
+ } ;
114
+ }
115
+ catch
116
+ {
117
+ throw new AuthenticationException ( "Invalid username or password." ) ;
115
118
}
116
119
}
117
120
}
0 commit comments