@@ -19,6 +19,7 @@ public class ArangoHttpTransport(IArangoConfiguration configuration) : IArangoTr
19
19
{
20
20
private static readonly HttpClient DefaultHttpClient = new ( ) ;
21
21
string _auth = "" ;
22
+ DateTime _authValidUntil = DateTime . MinValue ;
22
23
/// <inheritdoc />
23
24
protected string Auth
24
25
{
@@ -29,13 +30,35 @@ protected string Auth
29
30
return _auth ;
30
31
}
31
32
}
33
+ private async Task Authenticate ( bool auth , CancellationToken cancellationToken )
34
+ {
35
+ if ( string . IsNullOrWhiteSpace ( configuration . User ) )
36
+ return ;
32
37
38
+ if ( auth && ( _auth == null || _authValidUntil < DateTime . UtcNow ) )
39
+ {
40
+ var authResponse = await SendAsync < AuthResponse > ( HttpMethod . Post ,
41
+ "/_open/auth" ,
42
+ new AuthRequest
43
+ {
44
+ Username = configuration . User ,
45
+ Password = configuration . Password ?? string . Empty
46
+ } , auth : false , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
47
+
48
+ var jwt = authResponse . Jwt ;
49
+ var token = new JwtSecurityToken ( jwt . Replace ( "=" , "" ) ) ;
50
+ _auth = $ "Bearer { jwt } ";
51
+ _authValidUntil = token . ValidTo . AddMinutes ( - 5 ) ;
52
+ }
53
+ }
33
54
/// <inheritdoc />
34
55
public async Task < T > SendAsync < T > ( HttpMethod m , string url , object body = null ,
35
56
string transaction = null , bool throwOnError = true , bool auth = true ,
36
57
IDictionary < string , string > headers = null ,
37
58
CancellationToken cancellationToken = default )
38
59
{
60
+ await Authenticate ( auth , cancellationToken ) . ConfigureAwait ( false ) ;
61
+
39
62
using var req = new HttpRequestMessage ( m , configuration . Server + url ) ;
40
63
ApplyHeaders ( transaction , auth , req , headers ) ;
41
64
0 commit comments