Drkcore

01 03 2012 openid Node.js Express Tweet

ExpressでOpenID認証

イントラにOpenIDを入れたので認証のことを考えなくてもよくなって快適なので、色々作っている。

ちょっとsocket.ioを使いたかったので、Expressでopenid認証させるサンプルを書いてみた。node-openidを使っている。openid.ne.jpのID使って試してある。mixi-openidは動かなかったし、facebookとgoogleのopenidはドキュメントの場所がよくわからなくてイラッと来てやめた。

app.coffee

authenticateっていうパスにアクセスするとopen-id認証してverifyにリダイレクトされてくるようになっている。

express = require("express")
routes = require("./routes")
openid = require('openid')
url = require('url')
querystring = require('querystring')
relyingParty = new openid.RelyingParty(
  'http://localhost:3000/verify',
  null,
  false,
  false,[])

app = module.exports = express.createServer()
app.configure ->
  app.set "views", __dirname + "/views"
  app.set "view engine", "jade"
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use app.router
  app.use express.static(__dirname + "/public")

app.configure "development", ->
  app.use express.errorHandler(
    dumpExceptions: true
    showStack: true
  )

app.configure "production", ->
  app.use express.errorHandler()

app.get "/", routes.index

app.get "/authenticate", (req, res) ->
  identifier = req.query.openid_identifier
  relyingParty.authenticate identifier, false, (error, authUrl) ->
    if error
      res.send "Authentication failed: " + error
    else unless authUrl
      res.writeHead 200
      res.send "Authentication failed"
    else
      res.redirect(authUrl);

app.get "/verify", (req, res) ->
  relyingParty.verifyAssertion req, (error, result) ->
    if not error and result.authenticated
      res.send 'Success :)'
    else
      res.send 'Failure :('

app.listen 3000
console.log "Express server listening on port %d in %s mode",
  app.address().port, app.settings.env

view/index.jade

formを追加しただけ

h1= title
p Welcome to #{title}

form(method="get", action="/authenticate")
  p Login using OpenID
  input(name="openid_identifier")
  input(type="submit", value="Login")

イラッと来るのはsnow leopardにあげたらmacがやたらと重くなった部分も多い。さすがに4年も経つのでそろそろ買い換えてもいいかなと思ったりするところではある。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021