标签: passport-google-oauth2

如何区分 Google OAuth 中的实际图片与默认自动生成的图片?

我正在使用 Passport Google 策略 ( OAuth 2.0 ) 从 Google 检索用户数据。除非登录用户没有明确设置的个人资料图片,否则这工作正常。在这种情况下,Google 会返回带有用户名字首字母的默认图像:

在此输入图像描述

我希望能够判断 API 返回的图像是真实图像还是类似的图像。我该如何实现这个目标?这里有一个类似的问题,但答案已经过时了,因为谷歌似乎不再提供isDefault标志。它也没有一致的默认轮廓URI ,因为自动生成的图像对于不同的用户来说是不同的,具体取决于他们的姓名首字母。Google OAuth 2.0 是否提供任何替代标志来告诉我该图像是否是自动生成的?

google-oauth passport-google-oauth2

6
推荐指数
1
解决办法
485
查看次数

如何同时使用 bearer 和 oauth2 Passport.js 策略?

最近我开始使用 Passport.js 和 oauth2 策略进行身份验证。起初,我使用会话,一切都很棒,但我希望我的 API 是无状态的。我发现唯一可以帮助我解决这个问题的是承载策略(passport-http-bearer)。我找不到任何同时使用这两种策略的好例子,所以我有点迷失。也许我的方式是错误的。让我解释一下我想要做什么。

假设我已经配置了 google 策略(passport-google-oauth2),如下所示:

  passport.use(
    new GoogleStrategy(
      {
        clientID: <googleClientId>,
        clientSecret: <googleClientSecret>,
        callbackURL: `localhost:4000/auth/google/callback`,
        scope: ["profile", "email"],
      },
      async function (_accessToken, _refreshToken, profile, done) { // this is the verify function
        let user = <create new user>
        return done(null, user);
      }
    )
  );
Run Code Online (Sandbox Code Playgroud)

此路由将用户重定向到谷歌,他们将在其中进行身份验证:

app.get("/auth/google", passport.authenticate("google", { session: false }));
Run Code Online (Sandbox Code Playgroud)

这个处理响应并让用户登录:

app.get(
  "/google/callback",
  passport.authenticate("google", {
    session: false,
  })
);
Run Code Online (Sandbox Code Playgroud)

Google 策略发出一个不记名令牌,我想将该令牌返回给用户,以便我可以将其存储在客户端的 localStorage 中,并将其发送到Authorization每个请求的标头中以对用户进行身份验证。我的第一个问题是如何以及在哪里?我可以访问策略验证令牌中的令牌,但我不知道应该如何在响应正文中将其返回给用户。

我使用承载策略(passport-http-bearer)保护需要经过身份验证的用户的路由。我已经这样配置了:

passport.use(
  new BearerStrategy(async function (token, done) { // …
Run Code Online (Sandbox Code Playgroud)

authentication node.js oauth-2.0 passport.js passport-google-oauth2

6
推荐指数
1
解决办法
824
查看次数

如何从通行证回调中提取其他 API 范围?

我想通过策略获取使用 Passport.js 的用户的生日passport-google-oauth20。我从哪里获得这个额外范围的响应以及如何提取这些信息?

我在从用户那里提取个人资料和电子邮件方面取得了成功,但我尝试的所有其他范围似乎都找不到它返回的位置。我正在使用Google Identity Platform 中的一些范围(更具体地说,我已经从 Google Cloud Platform 激活了 People API)并且它们没有出现在 Passport 策略回调中(只有个人资料和电子邮件)。

这是我设置护照并检查返回的内容的地方

passport.use(
  new GoogleStrategy(config, (accessToken, refreshToken, profile, done) => {
    console.log(profile);
    // outputs:
    // id: ...
    // displayName: ...
    // name: { ... }
    // emails: [ { ... } ]
    // photos: [ { ... } ]
    // _raw: ...
    // _json: ... (above info plus profile link and locale)

    ...
  })
);
Run Code Online (Sandbox Code Playgroud)

在我的路线中,我声明了范围并重定向用户

router.get('/auth/google/', passport.authenticate('google', {
    scope: ['https://www.googleapis.com/auth/user.birthday.read', 'profile', …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express passport.js passport-google-oauth2

5
推荐指数
0
解决办法
243
查看次数

护照谷歌 oauth2 与护照谷歌 oauth20 包

这两个包看起来非常相似:

http://www.passportjs.org/packages/passport-google-oauth2/ http://www.passportjs.org/packages/passport-google-oauth20/

是一个取代另一个还是它们服务于不同的目的?Google 身份验证的新手,并且仍在尝试使其正常工作。

node.js passport.js passport-google-oauth2

5
推荐指数
2
解决办法
615
查看次数

仅登录 1 个谷歌帐户时,Passport Google Oauth2 不提示选择帐户

我正在尝试对我的节点 + nestjs api 中的用户进行身份验证,并希望提示用户选择一个帐户。

如果您只登录了 1 个帐户,则不会显示提示,即使您使用 2 个帐户登录并收到提示,重定向中的 URL 的参数中仍然包含 &prompt=none。

事实上,我可以确认提示选项没有区别。

我的代码简化如下:

import { OAuth2Strategy } from "passport-google-oauth";
import { PassportStrategy } from "@nestjs/passport";
@Injectable()
export class GoogleStrategy extends PassportStrategy(OAuth2Strategy, "google") {
  constructor(secretsService: SecretsService) {
    super({
      clientID: secretsService.get("google", "clientid"),
      clientSecret: secretsService.get("google", "clientsecret"),
      callbackURL: "https://localhost:3000/auth/google/redirect",
      scope: ["email", "profile", "openid"],
      passReqToCallback: true,
      prompt: "select_account",
    });
  }

  async validate(req: Request, accessToken, refreshToken, profile, done) {
    const { name, emails, photos } = profile;
    const user = {
      email: emails[0].value,
      firstName: …
Run Code Online (Sandbox Code Playgroud)

node.js passport.js passport-google-oauth passport-google-oauth2 nestjs

5
推荐指数
2
解决办法
919
查看次数

使用passport-google-oauth20时自动登录

我遵循了一门课程,它使用护照、passport-google-oauth20、cookie-session 实现了用户身份验证,并且一切正常(登录、注销、会话处理),但是当我发送登录/注册请求时,它不会询问/prompt 谷歌认证窗口输入凭据,它总是使用相同的帐户登录。

这是护照策略配置:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});

passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback',
      proxy: true,
      authorizationParams: {
        access_type: 'offline',
        approval_prompt: 'force'
      }
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleID: profile.id })
        if (existingUser) { …
Run Code Online (Sandbox Code Playgroud)

node.js cookie-session passport-google-oauth2

3
推荐指数
1
解决办法
1210
查看次数

未使用Fetch API调用进行身份验证的端点(使用passport-google-oauth2)

我有护照设置使用谷歌策略,可以直接到/ auth /谷歌伟大.我目前拥有它,以便当您使用google身份验证oauth2登录时,我的端点将通过检查a进行身份验证req.user.当我刚刚访问浏览器中的端点时,这可以正常工作.如果我去/auth/google,然后/questions,我将能够提出要求.但是当我尝试从redux发出一个获取请求时,我会收到一条错误消息Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0.它出现是因为fetch API试图进入我的/questions端点,通过我的loggedIn中间件,然后不满足if (!req.user)并重新定向.有关如何使用PassportJS和passport-google-oauth2从Fetch API进行身份验证的任何想法?

loggedIn函数:

function loggedIn(req, res, next) {
  if (req.user) {
    next();
  } else {
    res.redirect('/');
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的'GET'端点的代码.

router.get('/', loggedIn, (req, res) => {
  const userId = req.user._id;

  User.findById(userId, (err, user) => {
    if (err) {
      return res.status(400).json(err);
    }

    Question.findById(user.questions[0].questionId, (err, question) => {
      if (err) {
        return …
Run Code Online (Sandbox Code Playgroud)

node.js passport.js redux fetch-api passport-google-oauth2

2
推荐指数
1
解决办法
1352
查看次数

Passport for Facebook和Google的所有可能的范围选项

我无法在facebook和google的护照中找到所有可能的范围选项.任何人都可以提到我们在Facebook和谷歌认证期间可以传递的所有可能的范围选项/值吗?

我需要使用facebook和google的其他范围选项,就像我目前使用'email'范围值一样.请分别提及facebook和google的所有可能的范围选项.

passport.authenticate(key, {scope: ['email']})(req, res, next);
Run Code Online (Sandbox Code Playgroud)

node.js passport-facebook passport-local passport.js passport-google-oauth2

1
推荐指数
1
解决办法
1924
查看次数

无法使用护照和谷歌oauth策略访问谷歌登录页面

我正在尝试使用 Passport 让用户能够使用他们的 Google 帐户登录网络应用程序,但我似乎无法获取我的登录路由 /auth/google,甚至无法重定向到 Google 登录页面。我的其他路线工作正常,我的控制台中没有收到任何错误,但是当我转到 localhost:5000/auth/google 时,页面只是挂起并最终给出“localhost 拒绝连接”错误(我假设之后已超时)。

知道会发生什么吗?我已经在另一个应用程序中成功地使用了基本上完全相同的代码 - 我知道我还没有设置大部分用于完全登录的脚手架,但我认为此时至少应该加载谷歌登录页面。

索引.js

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import passport from 'passport';
const GoogleStrategy = require('passport-google-oauth20').Strategy;

// import database from './server/src/models';
import userRoutes from './server/routes/UserRoutes';

const PORT = process.env.PORT | 5000;
const app = express();

passport.use(
  new GoogleStrategy(
    {
      clientID: process.env.googleClientID,
      clientSecret: process.env.googleClientSecret,
      callbackURL: '/auth/google/callback'
    },
    (accessToken, refreshToken, profile, cb) => {
      console.log(accessToken);
    }
  )
);

app.use(cors());
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());

app.get('/', …
Run Code Online (Sandbox Code Playgroud)

node.js express google-oauth passport.js passport-google-oauth2

1
推荐指数
1
解决办法
3543
查看次数