GithubHelp home page GithubHelp logo

steam-openid's Introduction

steam-openid

Example

import SteamOpenID from './src'
import {Server} from 'http'

let sessionid = 0
const sessions = new Map

async function startSteamOpenIDHTTPServer(port) {
    const baseURL = 'http://localhost:' + port
    const returlURL = baseURL + '/claim'
    const steamSignIn = new SteamOpenID(returlURL)
    const authUrl = await steamSignIn.getAuthURL()

    new Server().on('request', async (req, res) => {
        const url = new URL(req.url, 'http://localhost:9090')
        console.log('>', url.toString())
        switch (url.pathname) {

            case '/': {
                const sessionidMatch = req.headers.cookie.match(/session=(\d+)/)
                const steamid = sessionidMatch && sessions.get(Number(sessionidMatch[1]))
                if(steamid) return res.end(
                    '<p>your verified steamid: ' + steamid +
                    '</p><a href="/logout">logout</a>')
                return res.end(`<a href=/logon>logon</a>`)
            }

            case '/logon': { //user wants to logon, redirecting to auth url steam gave us
                return res.writeHead(302, {
                    Location: authUrl
                }).end()
            }

            case '/claim': { //user returned (redirected) from auth url with url params which we should verify
                const steamid = await steamSignIn.verifyAssertion(url.toString())
                sessions.set(++sessionid, steamid)
                return res.writeHead(302, {
                    Location: '/',
                    'Set-Cookie': 'session=' + sessionid
                }).end()
            }

            case '/logout': {
                const sessionidMatch = req.headers.cookie.match(/session=(\d+)/)
                sessionidMatch && sessions.delete(Number(sessionidMatch[1]))
                return res.writeHead(302, {
                    Location: '/',
                    'Set-Cookie': 'session=; expires=Thu, 01 Jan 1970 00:00:00 GMT'
                }).end()
            }

        }
    })
        .listen(port)
        .on('listening', () => console.log(baseURL))
}

startSteamOpenIDHTTPServer(9090)

steam-openid's People

Contributors

waspyro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.