簡易認証 @ Ruby - Linux Life

はじめに

ユーザー名とパスワードによる簡易認証CGIを作ってみましょう。

password.cgi

ユーザーを管理するファイルです。 一行にユーザ名とパスワードをタブ区切りで記述します。 ブラウザからファイルを直接見られないように拡張子はcgiにしておきます。

user1    pass1
user2    pass2

target.cgi

認証に成功したときに表示するHTMLです。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="ja" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>内容画面</title>
    </head>
    <body>
        <p>
            認証に成功しました。
        </p>
        <p>
            <a href="./">簡易認証</a>に戻る
        </p>
    </body>
</html>

auth.html

認証に成功したときに表示するHTMLです。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="ja" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>認証画面</title>
    </head>
    <body>
<!-- message_begin -->
        <p>
            (    message    )
        </p>
<!-- message_end -->
        <form action="auth.cgi" method="post">
            <p>
            Name:<input type="text" name="user" value="(    user    )" />
            Pass:<input type="password" name="pass" value="(    pass    )" />
            <input type="submit" value="Go" />
            </p>
        </form>
        <p>
            <a href="./">簡易認証</a>に戻る
        </p>
    </body>
</html>

auth.cgi

認証に成功した場合は内容を表示し、それ以外の時は認証画面を表示します。 クッキーを利用し、次回以降は自動で認証を試みるようにしてあります。

#!/usr/bin/ruby -Ke
require 'cgi'
require 'htmltemplate'
def page
    html = HTMLTemplate.open( 'target.cgi' )
    html.to_s
end
def auth( user, pass )
    File.open( 'password.cgi' ) do |file|
        file.each do |line|
            u, p = line.chomp.split( /\t/ )
            if user == u and pass == p
                return page
            end
        end
    end
    show( user, pass, '認証に失敗しました。' )
end
def show( user, pass, message = nil )
    html = HTMLTemplate.open( 'auth.html' )
    html.block( 'message' ) do |block|
        message ? block.dup.gsub!( "(\tmessage\t)", message ) : ''
    end
    html.gsub!( "(\tuser\t)", user.to_s )
    html.gsub!( "(\tpass\t)", pass.to_s )
    html.to_s
end
cgi = CGI.new()
user = cgi.cookies['user'].first
user = cgi.params['user'].first if cgi.params['user'].first
user = CGI::escapeHTML( user ) if user
pass = cgi.cookies['pass'].first
pass = cgi.params['pass'].first if cgi.params['pass'].first
pass = CGI::escapeHTML( pass ) if pass
expires = Time.now + 60*60*24*30
cookies = []
cookies.push CGI::Cookie::new( { 'name' => 'user', 'value' => user, 'expires' => expires} )
cookies.push CGI::Cookie::new( { 'name' => 'pass', 'value' => pass, 'expires' => expires} )
cgi.out( 'cookie' => cookies ) do
    if user and pass
        auth( user, pass )
    else
        show( user, pass )
    end
end

確認

認証

おわりに

シンプルですが、簡易認証としては使いやすいと思います。 ログを記録する、パスワードを管理するなど色々応用してみてください。 自動認証用のクッキーをクリアしたい場合は以下をクリック。 auth.cgi?user=&pass=

スポンサードリンク

Copyright © 2025 Linux-Life.net