Beispiel #1
0
 internal MASUser(Configuration config, MASDevice device)
 {
     _config        = config;
     _device        = device;
     _storage       = new SecureStorage();
     _sharedStorage = new SharedSecureStorage();
 }
Beispiel #2
0
 internal MASDevice(Configuration config)
 {
     _config            = config;
     _storage           = new SecureStorage();
     _sharedStorage     = new SharedSecureStorage();
     _certManager       = new CertManager(_sharedStorage);
     _deviceInfoStorage = new SharedSecureStorage("DeviceInfoStorage");
 }
Beispiel #3
0
        async Task RemoveAccessTokensAsync()
        {
            ClearAllTokens();

            await SharedSecureStorage.RemoveAsync(_isAnonymous?StorageKeyNames.ClientAccessInfo : StorageKeyNames.UserAccessInfo);

            await SecureStorage.RemoveAsync(_isAnonymous?StorageKeyNames.ClientAccessInfo : StorageKeyNames.UserAccessInfo);
        }
        internal async Task ResetAsync()
        {
            Logger.LogInfo("Framework reseting...");

            MASDevice.Reset();

            MASUser.Reset();

            await SecureStorage.ResetAsync();

            await SharedSecureStorage.ResetAsync();

            await CertManager.UninstallAsync();

            HttpRequestFactory.CancelAll();

            Logger.LogInfo("Framework reset");
        }
Beispiel #5
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;
            }
        }