簡體   English   中英

帶有Node.js API和標准JavaScript應用的OAuth2

[英]OAuth2 with nodejs api and standard javascript app

我正在嘗試創建一種插件,用戶可以將其簡單地添加到網站中,它將對我的應用程序進行COR調用,並返回將由客戶端JavaScript處理的JSON。

這是我想要的方式,但是現在我試圖確保用戶登錄到我的應用程序,然后再允許其從服務器端應用程序接收任何JSON。

從這里開始,我將把Node.js API稱為服務器 ,將JS插件稱為Client。

我在節點上找到了一個N​​pm插件來處理服務器上的OAuth2,但是我不確定我是否真的了解如何使用它。 這是鏈接 ,我發現是在客戶端進行的。

客戶端->應用初始化程序:

define [
  'oauth2'
], (oauth2) ->
  App =
    Models: {}
    Collections: {}
    Views: {}

    initialize: () ->
      $.get "/javascripts/mu-config.json", (config) =>
        @api_url = config.api
        @site = config.site
        @credentials = config.credentials
        @make_oauth_call()


    make_oauth_call: ->
      @xhr = new oauth2.OAuth2XMLHttpRequest
        authorizeEndpoint: "#{this.api_url}/callback"
        tokenEndpoint: "#{this.api_url}/oauth/access_token"
        clientID: this.credentials.clientID
        clientSecret: this.credentials.clientSecret
        localStoragePrefix: "oauth2.#{this.site.name}"
        requestAuthorization: (callback) ->
          console.log 'what?'
          console.log callback


    @xhr.onreadystatechange =  () ->
      console.log "do something"


    @xhr.open "GET", "#{this.api_url}/notes?site=1&user=1"
    @xhr.setRequestHeader 'Content-type', 'application/x-www-form-urlencoded'
    @xhr.send "site=1&user=1"

那么什么在這里起作用? @xhr.open ...實際上確實可以從服務器獲取JSON,僅此而已。 我沒有從客戶端得到任何錯誤,但是console.log 'what?' 不會觸發,我也不相信任何東西會得到認證。

服務器-> oauth.coffee

  token = null
  credentials =
    clientID: "sparkmasterflex"
    clientSecret: "bob_the_builder"
    site: 'http://marking_up.dev'

  OAuth2 = require('simple-oauth2') credentials


  authorization_uri = OAuth2.AuthCode.authorizeURL
    redirect_uri: 'http://localhost:3000/callback'
    scope: 'sites'
    state: '55fce6241c8e6432e8dfee583141aa58'

  res.redirect(authorization_uri)

  OAuth2.AuthCode.getToken
    code: "something here"
    redirect_uri: "http://localhost:3000/callback"
  , saveToken

  saveToken = (error, result) ->
    console.log('Access Token Error', error.message) if error
    token = OAuth2.AccessToken.create(result)

  module.exports = OAuth2

服務器->路由器

express = require("express")
db = require "../database"
oauth2 = require "../oauth"

router = express.Router()

# GET home page.
router.get "/", (req, res) ->
  res.render 'index',
    title: "Hello world"

# Initial page redirecting to Github
router.get '/auth', (req, res) ->
  res.redirect authorization_uri


# Callback service parsing the authorization token and asking for the access token
# router.get '/callback', (req, res) ->
router.route('/callback')
  .get (req, res) ->
    code = req.query.code
    console.log '/callback'
    oauth2.AuthCode.getToken
      code: code
      redirect_uri: 'http://localhost:3000/callback'
    , saveToken

    saveToken = (error, result) ->
      console.log('Access Token Error', error.message) if error
      token = oauth2.AccessToken.create(result)

module.exports = router

運行節點服務器時出現此錯誤:

/Users/raymondke99/Sites/marking_up_api/oauth.js:19

res.redirect(authorization_uri);
^

ReferenceError: res is not defined
   at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:1)
   at Module._compile (module.js:456:26)
   at Object.Module._extensions..js (module.js:474:10)
   at Module.load (module.js:356:32)
   at Function.Module._load (module.js:312:12)
   at Module.require (module.js:364:17)
   at require (module.js:380:17)
   at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)
   at Module._compile (module.js:456:26)
   at Object.Module._extensions..js (module.js:474:10)

我有點不知所措。 這兩個文件的文檔看起來都很詳盡,但是我仍然覺得我缺少大量信息。 任何人都可以幫助和/或引導我提供幫助嗎?

謝謝

編輯

我從oauth.coffee中刪除了res.redirect() ,並收到以下錯誤消息:

/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16

  throw new Error('Callback not provided on API call');

    ^

Error: Callback not provided on API call
  at Object.api (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16:13)
  at Object.getToken (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/client/auth-code.js:34:8)
  at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:17)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)

我有一個以上的路由器,因為我使用的是expressjs,但我不確定應該在何處進行“全部捕獲”重定向。 是否需要進入每個路由器?

為什么宣誓文件中包含“ res.redirect(authorization_uri)”? 您似乎已經在路由器中擁有GET端點了?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM