LoginSignup
15
10

More than 5 years have passed since last update.

あなたのWebサービスでLDAPユーザ認証を実現するgo-ldapc

Last updated at Posted at 2016-07-24

go-ldapc はLDAPによるユーザ認証を実現するモジュールです。

GoDoc

go-ldapc ただ1つのAPIを提供しています。

実装例

LDAPによるユーザログインをかんたんに実装することができます。

  • Code:
import (
    "github.com/sonatard/ldapc"
)

func main() {
    ldapclient := &ldapc.Client{
        Protocol:  ldapc.LDAP,
        Host:      "localhost",
        Port:      389,
        TLSConfig: nil,
        Bind: &ldapc.AuthBind{
            BindDN:       "uid=user1,ou=People,dc=test,dc=com",
            BindPassword: "admin",
            BaseDN:       "dc=test,dc=com",
            Filter:       "(&(objectClass=posixAccount)(uid=%s))",
        },
    }

    username := "user2"
    password := "user2"

    entry, err := ldapclient.Authenticate(username, password)
    if err != nil {
        fmt.Printf("LDAP Authenticate failed: %v\n", err)
    }

    // Print all entry
    fmt.Printf("%+v\n", entry)

    // username and mail
    fmt.Printf("username: %v\n", entry.GetAttributeValue("uid"))
    fmt.Printf("mail: %v\n", entry.GetAttributeValue("mail"))
}
  • Output:
username: user2
mail: user2@test.com

匿名Bind、DirectBind、サーバがActiveDirectoryの場合などのその他の実装例はこちら ldapc_test.go.

デモ

  • OpenLDAP Serverを立てる

  • クライアント環境を実行

$ go get -v github.com/sonatard/go-ldapc
$ cd ${GOPATH}/src/github.com/sonatard/go-ldapc/example
$ go run main.go
username: user2
mail: user2@test.com

作った経緯

LDAPはディレクトリ検索サービスでありユーザ認証プロトコルではありません。そのためLDAPのBind,Searchなどのオペレーションを組み合わせてユーザ認証を実装する必要があります。

Go言語向けのLDAPライブラリのgo-ldapBindSearchを実行するAPIであり、WebサービスにLDAPユーザ認証を提供しようと思うと、かんたんに利用できるものではありませんでした。

そこでLDAPの勉強を兼ねて誰でも容易にLDAPユーザ認証を実現できるGo言語のモジュールを作りました。

また私は、社内では自由にPublicなWebサービスを使える環境ではないので、類似のOSS Webサービスを使うことが多いのですが、この時にLDAP認証がないとユーザ管理が辛いので、是非皆さんGo言語でWebサービスをOSSで作る際にはLDAP認証の提供をよろしくお願いします! :smiley:

参考

  • gogs/gogs - /gogs/modules/auth/ldapの実装を参考にしました。 gogs開発者の皆さんありがとうございます。
15
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
10