示例#1
0
        static async Task <Dictionary <string, string> > SetupRequestHeaders(MASUser user, PropertyList givenHeaders, RequestType requestType, ResponseType responseType)
        {
            var deviceMagId = MASDevice.Current.MagId;
            var headers     = new Dictionary <string, string>
            {
                { "mag-identifier", deviceMagId }
            };

            var accessTokenHeaderValue = await user?.GetAccessHeaderValueAsync();

            if (accessTokenHeaderValue != null)
            {
                headers[HttpHeaders.Authorization] = accessTokenHeaderValue;
            }

            if (givenHeaders != null)
            {
                foreach (var header in givenHeaders)
                {
                    headers[header.Key] = header.Value;
                }
            }

            if (responseType != ResponseType.Unknown)
            {
                headers[HttpHeaders.Accept] = ToHeaderValue(responseType);
            }

            if (requestType != RequestType.None)
            {
                headers[HttpHeaders.ContentType] = ToHeaderValue(requestType);
            }

            return(headers);
        }
示例#2
0
        static async Task <MASUser> LoginInternalAsync(string username, string password)
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

            if (!MASDevice.Current.IsRegistered && MAS.RegistrationKind == RegistrationKind.User)
            {
                if (Current != null)
                {
                    await Current.RemoveAccessTokensAsync();
                }

                await MASDevice.Current.RegisterWithUserAsync(username, password);
            }

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

            if (Current != null && Current.IsLoggedIn)
            {
                ErrorFactory.ThrowError(ErrorCode.UserAlreadyAuthenticated);
            }

            if (!MASDevice.Current.IsRegistered)
            {
                if (MAS.RegistrationKind == RegistrationKind.User)
                {
                    Logger.LogInfo("User device registration starting...");
                    await MASDevice.Current.RegisterWithUserAsync(username, password);

                    Logger.LogInfo("User device registration complete");
                }
                else
                {
                    ErrorFactory.ThrowError(ErrorCode.DeviceNotRegistered);
                }
            }

            var user = new MASUser(MASApplication.Current.Config, MASDevice.Current);
            await user.RequestAccessTokenAsync(username, password);

            Current = user;

            return(user);
        }
示例#3
0
        internal static async Task <MASUser> InitializeAsync(Configuration config, MASDevice device, bool isAnonymous)
        {
            var user = new MASUser(config, device);

            user._isAnonymous = isAnonymous;

            await user.LoadAsync();

            if (!isAnonymous && user.IsLoggedIn)
            {
                MASUser.Current = user;
            }

            return(user);
        }
示例#4
0
        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");
        }
示例#5
0
        static async Task <TextResponse> RequestHttpAsync(HttpMethod method, string endPointPath,
                                                          PropertyCollection parameters, PropertyCollection headers,
                                                          RequestType requestType, ResponseType responseType, bool attemptTokenRefresh)
        {
            if (!MASApplication.IsRegistered)
            {
                ErrorFactory.ThrowError(ErrorCode.ApplicationNotRegistered);
            }

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

            string url  = endPointPath;
            string body = null;

            if (method == HttpMethod.GET || method == HttpMethod.DELETE)
            {
                var builder = new HttpUrlBuilder(endPointPath);
                if (parameters != null)
                {
                    foreach (var paramInfo in parameters.Properties)
                    {
                        builder.Add(paramInfo.Key, paramInfo.Value);
                    }
                }

                url = builder.ToString();
            }
            else if (parameters != null)
            {
                body = FormatBody(requestType, parameters.Properties);
            }


            MASUser requestUser = null;

            if (MASUser.Current != null && MASUser.Current.IsLoggedIn)
            {
                requestUser = MASUser.Current;
            }
            else if (MASApplication.Current.Client != null)
            {
                requestUser = MASApplication.Current.Client;
            }

            var requestHeaders = await SetupRequestHeaders(requestUser, headers?.Properties, requestType, responseType);

            try
            {
                var requestResponse = await HttpRequestFactory.RequestTextAsync(new HttpRequestInfo()
                {
                    Url         = url,
                    Method      = method,
                    Headers     = requestHeaders,
                    Body        = body,
                    Certificate = MASDevice.Current.Certificate
                });

                return(ToMASResponse(requestResponse));
            }
            catch (MASException exp)
            {
                if (requestUser == null || attemptTokenRefresh == false || exp.MASErrorCode != ErrorCode.TokenAccessExpired)
                {
                    throw exp;
                }
            }

            // Our token has expired, attempt to refresh it and try again!
            await requestUser.RefreshAccessTokenAsync();

            // Lets not try to refresh token after our first attempt
            return(await RequestHttpAsync(method, endPointPath, parameters,
                                          headers, requestType, responseType, false));
        }
示例#6
0
        internal async Task StartAsync(string configContent, RegistrationKind regKind)
        {
            Logger.LogInfo("Framework starting...");

            // Load and validate configuration data
            var config = new Configuration();

            config.Load(configContent);
            _config = config;

            var clientId = config.DefaultClientId;

            Organization        = config.OAuth.Client.Organization;
            Name                = config.OAuth.Client.ClientName;
            DetailedDescription = config.OAuth.Client.Description;

            Identifier    = config.DefaultClientId.Id;
            Environment   = config.DefaultClientId.Environment;
            RegisteredBy  = config.DefaultClientId.RegisteredBy;
            Scope         = new List <string>(config.DefaultClientId.Scope.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s))).AsReadOnly();
            ScopeAsString = config.DefaultClientId.Scope;
            Status        = config.DefaultClientId.Status;

            RedirectUri = config.DefaultClientId.RedirectUri;

            // Load device and any previous registration info.
            await MASDevice.InitializeAsync(config);

            // Authorization providers not supported yet
            //var response = await MAGRequests.GetAuthorizationProviders(_config, Device);

            // Load client access if client registration is requested
            if (MAS.RegistrationKind == RegistrationKind.Client)
            {
                Client = await MASUser.InitializeAsync(config, MASDevice.Current, true);
            }

            // Load user and any previous access token or idtoken info
            await MASUser.InitializeAsync(config, MASDevice.Current, false);

            if (!MASDevice.Current.IsRegistered)
            {
                switch (regKind)
                {
                case RegistrationKind.Client:
                    if (!config.HasScope(ScopeNames.MssoClientRegister))
                    {
                        ErrorFactory.ThrowError(ErrorCode.DeviceRegistrationAttemptedWithUnregisteredScope);
                    }

                    await MASDevice.Current.RegisterWithClientAsync();

                    // Create a device anonymous user
                    var clientUser = new MASUser(_config, MASDevice.Current);
                    await clientUser.LoginAnonymouslyAsync();

                    Client = clientUser;

                    break;

                case RegistrationKind.User:
                    // Ask for login with user device registration
                    LoginRequested?.Invoke(null, EventArgs.Empty);
                    break;

                default:
                    ErrorFactory.ThrowError(ErrorCode.DeviceRegistrationAttemptedWithUnregisteredScope);
                    break;
                }
            }

            Logger.LogInfo("Framework started");
        }