Exemple #1
0
 private void OnTokenManagerStatusUpdateEvent(TokenManagerStatusUpdateEventArgs args)
 {
     if (TokenManagerStatusUpdateEvent != null)
     {
         TokenManagerStatusUpdateEvent.Invoke(this, args);
     }
 }
Exemple #2
0
        private Token Authenticate(OAuthCredentials credentials)
        {
            if (credentials == null)
            {
                throw new AuthFailedException("Missing credentials");
            }

            SecucardTrace.Info("Authenticate credentials: {0}", credentials.ToString());

            var            pollInterval       = 0;
            var            timeout            = DateTime.Now;
            var            devicesCredentials = credentials as DeviceCredentials;
            var            isDeviceAuth       = (devicesCredentials != null);
            DeviceAuthCode codes = null;


            // if DeviceAuth then get codes an pass to app thru event. Further action required by client
            if (isDeviceAuth)
            {
                codes = _rest.GetDeviceAuthCode(devicesCredentials.ClientId, devicesCredentials.ClientSecret, devicesCredentials.DeviceId);
                if (TokenManagerStatusUpdateEvent != null)
                {
                    TokenManagerStatusUpdateEvent.Invoke(this,
                                                         new TokenManagerStatusUpdateEventArgs
                    {
                        DeviceAuthCodes = codes,
                        Status          = AuthStatusEnum.Pending
                    });
                }

                SecucardTrace.Info("Retrieved codes for device auth: {0}, now polling for auth.", codes);

                // set poll timeout, either by config or by expire time of code
                var t = codes.ExpiresIn;
                if (t <= 0 || _config.AuthWaitTimeoutSec < t)
                {
                    t = _config.AuthWaitTimeoutSec;
                }
                timeout = DateTime.Now.AddSeconds(t * 1000);

                pollInterval = codes.Interval;
                if (pollInterval <= 0)
                {
                    pollInterval = 5; // poll default 5s
                }

                devicesCredentials.DeviceCode = codes.DeviceCode;
                devicesCredentials.DeviceId   = null; // device id must be empty for next auth. step!
            }


            do
            {
                Token token = null;
                if (isDeviceAuth)
                {
                    // in case of device auth, check for cancel and delay polling
                    if (CancelAuthFlag)
                    {
                        throw new AuthCanceledException("Authorization canceled by request.");
                    }
                    Thread.Sleep(pollInterval * 1000);

                    token = _rest.ObtainAuthToken(codes.DeviceCode, devicesCredentials.ClientId,
                                                  devicesCredentials.ClientSecret);
                    if (token == null) // auth not completed yet
                    {
                        OnTokenManagerStatusUpdateEvent(new TokenManagerStatusUpdateEventArgs
                        {
                            DeviceAuthCodes = codes,
                            Status          = AuthStatusEnum.Pending
                        });
                    }
                }
                else
                {
                    var clientCredentials = credentials as ClientCredentials;
                    if (clientCredentials != null)
                    {
                        token = _rest.GetToken(clientCredentials.ClientId, clientCredentials.ClientSecret);
                    }
                }

                if (token != null)
                {
                    return(token);
                }
            } while (DateTime.Now < timeout);

            if (isDeviceAuth)
            {
                throw new AuthTimeoutException();
            }

            throw new System.Exception("Unexpected failure of authentication.");
        }