Exemplo n.º 1
0
        async Task UnregisterInternalAsync()
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            if (!IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            try
            {
                await MAGRequests.UnregisterDevice(_config, this);

                if (Certificate != null)
                {
                    await CertManager.UninstallAsync();

                    Certificate        = null;
                    RegisteredUsername = null;
                }

                await MASApplication.Current.ResetAsync();
            }
            catch (Exception e)
            {
                ErrorFactory.ThrowError(ErrorCode.DeviceCouldNotBeDeregistered, e);
            }
        }
Exemplo n.º 2
0
        async Task LogoffInternalAsync()
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            if (!MASDevice.Current.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.DeviceNotRegistered);
            }

            if (Current == null || !Current.IsLoggedIn)
            {
                ErrorFactory.ThrowError(ErrorCode.UserNotAuthenticated);
            }

            if (_idToken != null && _config.Mag.MobileSdk.IsSSOEnabled)
            {
                await MAGRequests.LogoutSessionAsync(_config, MASDevice.Current, this);
            }
            else
            {
                await MAGRequests.RevokeAccessTokenAsync(_config, _device, this);
            }

            await RemoveAccessTokensAsync();

            Current = null;
        }
Exemplo n.º 3
0
        internal async Task RefreshAccessTokenAsync()
        {
            // We have a refresh token, lets use that.
            if (_refreshToken != null)
            {
                RequestTokenResponseData data = null;
                try
                {
                    data = await MAGRequests.RefreshAccessTokenAsync(_config, _device, _refreshToken);
                }
                catch
                {
                    // TODO If this fails try to use idToken to get access token
                    if (_idToken != null && _idTokenType != null)
                    {
                        data = await MAGRequests.RequestAccessTokenFromIdTokenAsync(_config, _device, _idToken, _idTokenType);
                    }
                }

                await UpdateTokens(data);
            }
            // We have an id token, lets use that to request an access token.
            else if (_idToken != null && _idTokenType != null)
            {
                var data = await MAGRequests.RequestAccessTokenFromIdTokenAsync(_config, _device, _idToken, _idTokenType);
                await UpdateTokens(data);
            }
            // Lets check if we are anonymous, we can simply just login in again to get an access token.
            else if (_isAnonymous)
            {
                var data = await MAGRequests.RequestAccessTokenAnonymouslyAsync(_config, _device);
                await UpdateTokens(data);
            }
        }
Exemplo n.º 4
0
        internal async Task RegisterWithUserAsync(string username, string password)
        {
            var csr = await _certManager.GenerateCSRAsync(_config, this, username);

            var response = await MAGRequests.RegisterDeviceForUserAsync(_config, this, username, password, csr);

            await FinalizeRegistrationAsync(response, username);
        }
Exemplo n.º 5
0
        internal async Task RegisterWithClientAsync()
        {
            var username = "******";
            var csr      = await _certManager.GenerateCSRAsync(_config, this, username);

            var response = await MAGRequests.RegisterDeviceAsync(_config, this, csr);

            await FinalizeRegistrationAsync(response, username);
        }
Exemplo n.º 6
0
        internal async Task RequestAccessTokenAsync(string username, string password)
        {
            if (IsLoggedIn)
            {
                ErrorFactory.ThrowError(ErrorCode.UserAlreadyAuthenticated);
            }

            var data = await MAGRequests.RequestAccessTokenAsync(_config, _device, username, password);

            await UpdateTokens(data);
        }
Exemplo n.º 7
0
        internal async Task LoginAnonymouslyAsync()
        {
            if (IsLoggedIn)
            {
                ErrorFactory.ThrowError(ErrorCode.UserAlreadyAuthenticated);
            }

            _isAnonymous = true;

            var data = await MAGRequests.RequestAccessTokenAnonymouslyAsync(_config, _device);

            await UpdateTokens(data);

            _isAnonymous = true;
        }
Exemplo n.º 8
0
        async Task <IUserInfo> GetInfoInternalAsync()
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            if (!MASDevice.Current.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.DeviceNotRegistered);
            }

            if (!IsLoggedIn)
            {
                ErrorFactory.ThrowError(ErrorCode.UserNotAuthenticated);
            }

            try
            {
                return((IUserInfo)await MAGRequests.GetUserInfoAsync(_config, _device, this));
            }
            catch (Exception exp)
            {
                var masException = exp.InnerException as MASException;
                if (masException?.MASErrorCode != ErrorCode.TokenAccessExpired)
                {
                    throw exp;
                }
            }

            // Our access token is expired
            _accessToken = null;
            await SaveTokensAsync();

            await RefreshAccessTokenAsync();

            return((IUserInfo)await MAGRequests.GetUserInfoAsync(_config, _device, this));
        }
Exemplo n.º 9
0
        async Task LoadAsync()
        {
            //Since the device Id was different for different app of same publisher we had to fallback by saving the Device to the publisher shared storage and then share them between apps
            //Issue: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/78edc38b-41b9-4fe2-9bbf-20f282ddc25c/uwphow-to-make-2-apps-part-of-the-same-package?forum=wpdevelop
            Id = await _deviceInfoStorage.GetTextAsync(StorageKeyNames.DeviceId);

            if (string.IsNullOrEmpty(Id))
            {
                Id = CreateHardwareId();
                await _deviceInfoStorage.SetAsync(StorageKeyNames.DeviceId, Id);
            }

            // Install any certificate found in the server certs.
            // This is required for MAG SSL with alternative certificate authorities.
            if (_config.Server.ServerCerts != null)
            {
                foreach (var serverCert in _config.Server.ServerCerts)
                {
                    await _certManager.InstallTrustedServerCert(serverCert);
                }
            }

            var clientInfo = await _storage.GetTextAsync(StorageKeyNames.ClientInfo);

            string   clientId       = null;
            string   clientSecret   = null;
            DateTime?expirationDate = null;

            if (clientInfo != null)
            {
                try
                {
                    var jsonObj = JsonObject.Parse(clientInfo);

                    clientId       = jsonObj.GetNamedString("clientId");
                    clientSecret   = jsonObj.GetNamedString("clientSecret");
                    expirationDate = DateTime.FromBinary((long)jsonObj.GetNamedNumber("clientExpiration"));
                }
                catch
                {
                    clientId       = null;
                    clientSecret   = null;
                    expirationDate = null;
                }
            }

            if (clientId == null || clientSecret == null || expirationDate == null || DateTime.UtcNow >= expirationDate.Value)
            {
                var clientCredResponse = await MAGRequests.GetClientCredentialsAsync(_config, Id.ToBase64());

                _clientId     = clientCredResponse.ClientId;
                _clientSecret = clientCredResponse.ClientSecret;

                if (clientCredResponse.Expiration == 0) // 0 means never expire
                {
                    _clientExpiration = DateTime.MaxValue;
                }
                else
                {
                    _clientExpiration = clientCredResponse.Expiration.FromUnixTime();
                }

                JsonObject obj = new JsonObject();
                obj.SetNamedValue("clientId", JsonValue.CreateStringValue(_clientId));
                obj.SetNamedValue("clientSecret", JsonValue.CreateStringValue(_clientSecret));
                obj.SetNamedValue("clientExpiration", JsonValue.CreateNumberValue(_clientExpiration.ToBinary()));

                await _storage.SetAsync(StorageKeyNames.ClientInfo, obj.Stringify());
            }
            else
            {
                _clientId         = clientId;
                _clientSecret     = clientSecret;
                _clientExpiration = expirationDate.Value;
            }

            var deviceInfo = await _sharedStorage.GetTextAsync(StorageKeyNames.DeviceInfo);

            if (deviceInfo != null)
            {
                try
                {
                    var jsonObj = JsonObject.Parse(deviceInfo);

                    MagId  = jsonObj.GetNamedString("magId");
                    Status = jsonObj.GetNamedString("status");
                }
                catch
                {
                    MagId  = null;
                    Status = null;
                }
            }
            else
            {
                MagId  = null;
                Status = null;
            }

            // check if we have a certificate
            Certificate = await _certManager.GetIfExistsAsync();

            if (Certificate == null || DateTime.Now > Certificate.ValidTo)
            {
                RegisteredUsername = null;
                Certificate        = null;
                await SharedSecureStorage.RemoveAsync(StorageKeyNames.DeviceInfo);
            }
            else
            {
                RegisteredUsername = Certificate.Subject;
            }
        }