具有 github 授权的安全堆栈

SAFE-stack with github authorization

我正试图将我的整个应用程序隐藏在身份验证之后(我将在这有效时处理授权),现在我希望每个 url 都需要 github 登录。我没有打开 github 登录页面。

我尝试合并 SAFE-stack template and "Using OAuth with Saturn",但我没有得到 github 登录页面(当我只遵循 Saturn 指南时确实得到了),我只是得到了正常的待办事项页面.如果我点击添加按钮,服务器打印

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:8085/api/ITodosApi/addTodo application/json; charset=UTF-8 68
info: Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler`1[[Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions, Microsoft.AspNetCore.Authentication.OAuth, Version=3.1.11.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]][12]
      AuthenticationScheme: GitHub was challenged.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 10.8057ms 302 

并且该项目未添加到待办事项列表中。

我已经尝试去掉除身份验证位以外的所有内容,与干净安全的模板相比,我所做的唯一更改是

  1. dotnet paket add Saturn.Extensions.Authorization --project src/Server/Server.fsproj
  2. 手动降级 paket.lock 中的两个依赖项,否则会产生错误:Microsoft.AspNetCore.Authentication.Google(3.1.11)Microsoft.AspNetCore.Authentication.OpenIdConnect(3.1.11)
  3. Server/Server.fs 中的 app 值更改为以下内容(我为此问题创建了一个新的 github 身份验证应用程序):
let loggedInPipeline = pipeline {
    requires_authentication (Giraffe.Auth.challenge "GitHub")
}

let loggedInView = router {
    pipe_through loggedInPipeline
    get "/" webApp
}

let appRouter = router {
    forward "" loggedInView
}

let app =
    application {
        use_router appRouter
        url "http://0.0.0.0:8085/"
        memory_cache
        use_static "public"
        use_gzip
        use_github_oauth "8cde657dfd1d3a41b9ed" "0b245e12900ff8486ade076aae07aa0deb0fd83d" "/signin-github" [("login", "githubUsername"); ("name", "fullName")]
    }

run app

我的 gitHub 应用程序配置身份验证回调 url:http://localhost:8080/signin-github

您是否尝试过 https 而不是 http 作为您的 url 参数?

替换

url "http://0.0.0.0:8085"

url "https://0.0.0.0:8085"

这解决了我的问题。

演示代码:https://github.com/functionalfriday/fsharp-saturn-demos

我得到了解决此问题的帮助,仅针对 AzureAD。可以在此处 https://www.compositional-it.com/news-blog/safe-stack-authentication-with-active-directory-part-2/ 找到博客 post。 github 应该相同。我需要做的是对 webpack.config.jsServer.fsbuild.fsx

进行一些更改

webpack.config.js

devServerProxy: {
    // redirect all requests to the server on port 8085
    '**': {
//...
var CONFIG = {
    appHtmlTemplate: './src/Client/app.html',
//...
var commonPlugins = [
    new HtmlWebpackPlugin({
        filename: 'app.html',
//...
var CONFIG = {
    // ... other webpack config settings
    outputDir: './src/Server/public',
     // ...
    devServer: {
        // ...other dev server config settings
        writeToDisk: true
    },

Server.fs:

let authScheme = "AzureAD"

let isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") = Environments.Development;

let noAuthenticationRequired nxt ctx = task { return! nxt ctx }

let authChallenge : HttpFunc -> HttpContext -> HttpFuncResult =
    requiresAuthentication (Auth.challenge authScheme)

let routes =
    choose [
        route "/" >=> authChallenge >=>  htmlFile "public/app.html"
    ]

build.fsx:

let serverPublicPath = Path.getFullName "./src/Server/public"
let clientPublicPath = Path.getFullName "./src/Client/public"
Target.create "Clean" (fun _ ->
    Shell.cleanDir deployDir
    Shell.cleanDir serverPublicPath)
Target.create "Run" (fun _ ->
    Shell.copyDir serverPublicPath clientPublicPath FileFilter.allFiles
    //... other commands

如果您在本地工作,由于前面提到的 cookie 问题,您将需要使用非 Chrome 浏览器,例如 Firefox。

最好打开隐私浏览window,以确保您还没有登录帐户等

如果您有问题,请检查您是否有

  • 在 Azure 中正确设置应用程序的 Active Directory 注册
  • appsettings.json 中为您的服务器添加了所需的 AD 配置,包括您在 AD 注册中设置的登录/注销回调 url。