chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Threading;
using OpenTap.Authentication;
using OpenTap.Cli;
namespace OpenTap
{
    /// <summary> Sets the authentication token for use with a given domain.  </summary>
    //[Display("login",Description: "Log in to a online service. Only supports setting the token directly using '--token'.")]
    // LoginAction currently disabled, but kept around in case we need it in the future.
    class LoginAction// : ICliAction
    {
        /// <summary>  Gets or sets the domain. </summary>
        [UnnamedCommandLineArgument("url")]
        [Display("url")]
        
        public string Url { get; set; }
 
        /// <summary>  Gets or sets the token. </summary>
        [CommandLineArgument("token")]
        public string Token { get; set; }
       
        static readonly TraceSource log = Log.CreateSource("login");
        
        /// <summary> Logs in to the url. </summary>
        public int Execute(CancellationToken cancellationToken)
        {
            // We currently only support by 'logging in' by setting the token directly
            // using --token. In the future we can add support for letting the server tell us how to 
            // log in through the browser.
            if (string.IsNullOrEmpty(Token))
            {
                log.Error("Currently only --token is a supported method of login");
                return -1;
            }
            var uri = new Uri(Url, UriKind.RelativeOrAbsolute);
            var authority = uri.IsAbsoluteUri ? uri.Authority : Url;
            log.Debug($"Selecting {authority} as authority.");
            
            AuthenticationSettings.Current.Tokens.RemoveIf(x => x.Domain == authority);
            AuthenticationSettings.Current.Tokens.Add(new TokenInfo(Token, "", authority));
            AuthenticationSettings.Current.Save();
            log.Info($"Successfully set the authentication token for {Url}.");
            return 0;
        }
    }
}