Example #1
0
        public static void GenerateUrlsForOAuth(string clientId, string[] scopes, OAuthFlow flow, out string startUrl, out string completeUrl, string redirectUrl = OAuthDesktopEndPoint)
        {
            Dictionary <string, string> urlParam = new Dictionary <string, string>();

            urlParam.Add("client_id", clientId);
            urlParam.Add("scope", GenerateScopeString(scopes));
            urlParam.Add("redirect_uri", redirectUrl);
            urlParam.Add("display", "popup");

            switch (flow)
            {
            case OAuthFlow.ImplicitGrant:
                urlParam.Add("response_type", "token");
                break;

            case OAuthFlow.AuthorizationCodeGrant:
                urlParam.Add("response_type", "code");
                break;

            default:
                throw new NotSupportedException("flow value not supported");
            }

            startUrl    = BuildUriWithParameters(OAuthMSAAuthorizeService, urlParam);
            completeUrl = redirectUrl;
        }
Example #2
0
        public FormMicrosoftAccountAuth(string startUrl, string endUrl, OAuthFlow flow = OAuthFlow.AuthorizationCodeGrant)
        {
            InitializeComponent();

            this.StartUrl     = startUrl;
            this.EndUrl       = endUrl;
            this.AuthFlow     = flow;
            this.FormClosing += FormMicrosoftAccountAuth_FormClosing;
        }
        public OauthLoginWindow()
        {
            InitializeComponent();
            DataContext = this;

            OAuthFlower    = new OAuthFlow();
            XboxliveAuther = new XboxliveAuth();
            McServices     = new MinecraftServices();
        }
        public FormMicrosoftAccountAuth(string startUrl, string endUrl, OAuthFlow flow = OAuthFlow.AuthorizationCodeGrant)
        {
            InitializeComponent();

            this.StartUrl = startUrl;
            this.EndUrl = endUrl;
            this.AuthFlow = flow;
            this.FormClosing += FormMicrosoftAccountAuth_FormClosing;
        }
        /// <summary>
        /// Serializes a <see cref="OAuthFlow"/> value.
        /// </summary>
        /// <param name="value">The <see cref="OAuthFlow"/> value to serialize.</param>
        /// <returns>The <see cref="JsonValue"/>.</returns>
        protected virtual JsonValue SerializeOAuthFlow(OAuthFlow value)
        {
            if (value is null)
            {
                return(null);
            }

            var json = new JsonObject();

            SetJsonValue(json, PropertyConstants.AuthorizationUrl, value.AuthorizationUrl, true);
            SetJsonValue(json, PropertyConstants.TokenUrl, value.TokenUrl, true);
            SetJsonValue(json, PropertyConstants.RefreshUrl, value.RefreshUrl);
            SetJsonMap(json, PropertyConstants.Scopes, value.Scopes);

            return(json);
        }
Example #6
0
        public AuthResult(Uri resultUri, OAuthFlow flow)
        {
            this.AuthFlow = flow;

            string[] queryParams = null;
            switch (flow)
            {
                case OAuthFlow.ImplicitGrant:
                    int accessTokenIndex = resultUri.AbsoluteUri.IndexOf("#access_token");
                    if (accessTokenIndex > 0)
                    {
                        queryParams = resultUri.AbsoluteUri.Substring(accessTokenIndex + 1).Split('&');
                    }
                    else
                    {
                        queryParams = resultUri.Query.TrimStart('?').Split('&');
                    }
                    break;
                case OAuthFlow.AuthorizationCodeGrant:
                    queryParams = resultUri.Query.TrimStart('?').Split('&');
                    break;
                default:
                    throw new NotSupportedException("flow value not supported");
            }

            foreach (string param in queryParams)
            {
                string[] kvp = param.Split('=');
                switch (kvp[0])
                {
                    case "code":
                        this.AuthorizeCode = kvp[1];
                        break;
                    case "access_token":
                        this.AccessToken = kvp[1];
                        break;
                    case "authorization_token":
                    case "authentication_token":
                        this.AuthenticationToken = kvp[1];
                        break;
                    case "error":
                        this.ErrorCode = kvp[1];
                        break;
                    case "error_description":
                        this.ErrorDescription = Uri.UnescapeDataString(kvp[1]);
                        break;
                    case "token_type":
                        this.TokenType = kvp[1];
                        break;
                    case "expires_in":
                        this.AccessTokenExpiresIn = new TimeSpan(0, 0, int.Parse(kvp[1]));
                        break;
                    case "scope":
                        this.Scopes = kvp[1].Split(new string[] { "%20" }, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    case "user_id":
                        this.UserId = kvp[1];
                        break;
                }
            }
        }
Example #7
0
        public static IServiceCollection AddSwaggerDocs(this IServiceCollection services)
        {
            var settings = services.GetOptions <SwaggerSettings>("swagger");

            if (!settings.Enabled)
            {
                return(services);
            }

            services.AddSingleton(new SwaggerSettings());
            return(services.AddSwaggerGen(setup =>
            {
                setup.SwaggerDoc(settings.Name, new OpenApiInfo {
                    Title = settings.Title, Version = settings.Version
                });

                if (settings.CommentsEnabled)
                {
                    var filePath = Path.Combine(System.AppContext.BaseDirectory, "CourseLibrary.Api.xml");
                    setup.IncludeXmlComments(filePath);
                }

                if (settings.Authorization)
                {
                    setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Name = "Authorization",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                        Scheme = "Bearer",
                        Description = "JWT Authorization header using the Bearer scheme (Example: Bearer {token}).",
                    });

                    if (!(settings.OAuth2 is null))
                    {
                        setup.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                        {
                            Flows = new OpenApiOAuthFlows
                            {
                                Implicit = OAuthFlow.Setup(settings),
                                Password = OAuthFlow.Setup(settings),
                                ClientCredentials = OAuthFlow.Setup(settings),
                                AuthorizationCode = OAuthFlow.Setup(settings)
                            },
                            In = ParameterLocation.Header,
                            Name = "Authorization",
                            Type = SecuritySchemeType.OAuth2
                        });
                    }

                    setup.AddSecurityRequirement(new OpenApiSecurityRequirement()
                    {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference
                                {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                }
                            },
                            Array.Empty <string>()
                        }
                    });
                }
            }));
        }
        public AuthResult(Uri resultUri, OAuthFlow flow)
        {
            this.AuthFlow = flow;

            string[] queryParams = null;
            switch (flow)
            {
            case OAuthFlow.ImplicitGrant:
                int accessTokenIndex = resultUri.AbsoluteUri.IndexOf("#access_token");
                if (accessTokenIndex > 0)
                {
                    queryParams = resultUri.AbsoluteUri.Substring(accessTokenIndex + 1).Split('&');
                }
                else
                {
                    queryParams = resultUri.Query.TrimStart('?').Split('&');
                }
                break;

            case OAuthFlow.AuthorizationCodeGrant:
                queryParams = resultUri.Query.TrimStart('?').Split('&');
                break;

            default:
                throw new NotSupportedException("flow value not supported");
            }

            foreach (string param in queryParams)
            {
                string[] kvp = param.Split('=');
                switch (kvp[0])
                {
                case "code":
                    this.AuthorizeCode = kvp[1];
                    break;

                case "access_token":
                    this.AccessToken = kvp[1];
                    break;

                case "authorization_token":
                case "authentication_token":
                    this.AuthenticationToken = kvp[1];
                    break;

                case "error":
                    this.ErrorCode = kvp[1];
                    break;

                case "error_description":
                    this.ErrorDescription = Uri.UnescapeDataString(kvp[1]);
                    break;

                case "token_type":
                    this.TokenType = kvp[1];
                    break;

                case "expires_in":
                    this.AccessTokenExpiresIn = new TimeSpan(0, 0, int.Parse(kvp[1]));
                    break;

                case "scope":
                    this.Scopes = kvp[1].Split(new string[] { "%20" }, StringSplitOptions.RemoveEmptyEntries);
                    break;

                case "user_id":
                    this.UserId = kvp[1];
                    break;
                }
            }
        }
Example #9
0
        public static async Task <string> GetAuthenticationToken(string clientId, string[] scopes, OAuthFlow flow, IWin32Window owner = null)
        {
            string startUrl, completeUrl;

            GenerateUrlsForOAuth(clientId, scopes, flow, out startUrl, out completeUrl);

            FormMicrosoftAccountAuth authForm = new FormMicrosoftAccountAuth(startUrl, completeUrl, flow);
            DialogResult             result   = await authForm.ShowDialogAsync(owner);

            if (DialogResult.OK == result)
            {
                return(OnAuthComplete(authForm.AuthResult));
            }
            return(null);
        }
        public static async Task<string> GetAuthenticationToken(string clientId, string[] scopes, OAuthFlow flow, IWin32Window owner = null)
        {
            string startUrl, completeUrl;
            GenerateUrlsForOAuth(clientId, scopes, flow, out startUrl, out completeUrl);

            FormMicrosoftAccountAuth authForm = new FormMicrosoftAccountAuth(startUrl, completeUrl, flow);
            DialogResult result = await authForm.ShowDialogAsync(owner);
            if (DialogResult.OK == result)
            {
                return OnAuthComplete(authForm.AuthResult);
            }
            return null;
        }
        public static void GenerateUrlsForOAuth(string clientId, string[] scopes, OAuthFlow flow, out string startUrl, out string completeUrl, string redirectUrl = OAuthDesktopEndPoint)
        {
            Dictionary<string, string> urlParam = new Dictionary<string, string>();
            urlParam.Add("client_id", clientId);
            urlParam.Add("scope", GenerateScopeString(scopes));
            urlParam.Add("redirect_uri", redirectUrl);
            urlParam.Add("display", "popup");

            switch (flow)
            {
                case OAuthFlow.ImplicitGrant:
                    urlParam.Add("response_type", "token");
                    break;
                case OAuthFlow.AuthorizationCodeGrant:
                    urlParam.Add("response_type", "code");
                    break;
                default:
                    throw new NotSupportedException("flow value not supported");
            }

            startUrl = BuildUriWithParameters(OAuthMSAAuthorizeService, urlParam);
            completeUrl = redirectUrl;
        }