サーバを立てる
- DockerでOpenLdapを389で起動します
https://qiita.com/yuta_vamdemic/items/53cd67eea5af71ffd08a
認証する
Ldapモジュールをインストール
1 |
go get github.com/go-ldap/ldap |
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"crypto/tls"</span> <span class="s">"errors"</span> <span class="s">"fmt"</span> <span class="s">"github.com/go-ldap/ldap"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">ExampleconnSearch</span><span class="p">()</span> <span class="p">(</span><span class="kt">bool</span><span class="p">,</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span> <span class="n">ldapServer</span> <span class="o">:=</span> <span class="s">"ldap://localhost:389"</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">errors</span><span class="o">.</span><span class="n">New</span><span class="p">(</span><span class="s">"connection error"</span><span class="p">)</span> <span class="c">// ldapServerへの接続確認</span> <span class="n">l</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">ldap</span><span class="o">.</span><span class="n">DialURL</span><span class="p">(</span><span class="n">ldapServer</span><span class="p">,</span> <span class="n">ldap</span><span class="o">.</span><span class="n">DialWithTLSConfig </span><span class="p">(</span><span class="o">&</span><span class="n">tls</span><span class="o">.</span><span class="n">Config</span><span class="p">{</span><span class="n">InsecureSkipVerify</span><span class="o">:</span> <span class="no">true</span><span class="p">}))</span> <span class="c">// 接続失敗時のエラーハンドリング</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="s">"ldap connection error"</span><span class="p">)</span> <span class="k">return</span> <span class="no">false</span><span class="p">,</span> <span class="n">err</span> <span class="p">}</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="s">"ldap connection success"</span><span class="p">)</span> <span class="k">defer</span> <span class="n">l</span><span class="o">.</span><span class="n">Close</span><span class="p">()</span> <span class="k">return</span> <span class="no">true</span><span class="p">,</span> <span class="no">nil</span> <span class="p">}</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">ExampleconnSearch</span><span class="p">()</span> <span class="p">}</span> |
実行結果
1 2 |
$ go run main.go ldap connection success |