1

那里有类似的问题,但没有直接解决为什么这个 express-session 应用程序在 Safari 中始终有效,但在 chrome 中无效。

我的服务器代码在下面,它只需要验证一个带有注销和登录视图的页面。昨天,当本地服务和部署到 Azure 或 Heroku 时,它在 chrome 中工作。今天,在没有进行任何更改的情况下,无论是在本地还是部署,它都无法在 chrome 中运行。主页加载但是当我单击登录并重定向到 Okta 登录页面时,这是我在登录后重定向回我的页面时遇到的错误(例如 localhost:3000/authorization-code/callback?code=xxxxxxxx&state= xxxxxxxx) 登录后:

错误:未在会话中找到预期的授权请求详细信息,req.session["oidc:https://subdomain.domain.com/oauth2/default"] 未定义

然后我在 Safari 中进行了测试,它在本地和托管方面一直没有问题。我刚刚注意到托管版本暂时在 Azure 中再次运行 - 再次,没有更改或重新部署。本地版本还是不行。

const http = require('http')
const express = require('express')
const path = require('path')
const app = express()
const fs = require('fs')

require('dotenv').config()
app.use(express.json())
app.use(express.urlencoded({
  extended: true
}))
app.use(express.static('express'))
var cors = require('cors')
const OktaJwtVerifier = require('@okta/jwt-verifier')
const session = require('express-session')
const {
  ExpressOIDC
} = require('@okta/oidc-middleware')

var getUserInfo = require('./getUserInfo')

// session support is required to use ExpressOIDC
app.use(
  session({
    secret: 'secretsecret',
    resave: true,
    saveUninitialized: false,
    cookie: {
      httpOnly: false,
    },
  })
)

const oidc = new ExpressOIDC({
  issuer: process.env.ISSUER || 'https://[okta hosted custom domain].com/oauth2/default',
  client_id: process.env.CLIENT_ID || 'xxxxxxxxxx',
  client_secret: process.env.CLIENT_SECRET || 'xxxxxxxxxxxxxxxxxxx',
  redirect_uri: process.env.REDIRECT_URI ||
    'http://localhost:3000/authorization-code/callback',
  appBaseUrl: process.env.APP_BASE_URL || 'http://localhost:3000',
  scope: 'openid profile',
})

// ExpressOIDC attaches handlers for the /login and /authorization-code/callback routes
app.use(oidc.router)

app.use(cors())
app.options('*', cors())


app.get('/userinfo', (req, res) => {

  console.debug("in user info")

  let domain = 'dev'

  if (req.isAuthenticated()) {
    getUserInfo.userRequest(res, req.userContext, domain)
  }
})

app.get('/authStatus', (req, res) => {
  console.debug("in auth status")
  if (req.isAuthenticated()) {
    res.send(req.userContext.userinfo)
  }
})

app.post('/forces-logout', oidc.forceLogoutAndRevoke(), (req, res) => {
  // Nothing here will execute, after the redirects the user will end up wherever the `routes.logoutCallback.path` specifies (default `/`)
})

// default URL for website
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/index.html'))
  //__dirname : It will resolve to your project folder.
})

// FAQ Path
app.get('/help', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/help.html'))
  //__dirname : It will resolve to your project folder.
})

// default URL for website
app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/index.html'))
  //__dirname : It will resolve to your project folder.
})

const port = normalizePort(process.env.PORT || '3000')
const server = http.createServer(app)
server.listen(port)

console.debug('Info site server listening on port ' + port)

function normalizePort(val) {
  var port = parseInt(val, 10)

  if (isNaN(port)) {
    // named pipe
    return val
  }

  if (port >= 0) {
    // port number
    return port
  }

  return false
}
4

0 回答 0