2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Go】go-oidcを使ってOpen ID Connectを利用する

Posted at

Open ID Connect

  • outh2の進化版でトークン発行の処理がある
  • アプリケーション自体にユーザーパスワードなどの認証情報を持たせずに、別のプロバイダー(ここではGoogle)に委任する
  • プロバイダーはいろいろある
    • Google
    • AzureAD
    • AWS Cognito
    • outh0
  • stateという概念があり、クロスサイトスクリプティングなどのなりすましを防止

公式パッケージ

インストール

git cloen https://github.com/coreos/go-oidc

exampleのREADMEの通りにセットアップする

cd go-oidc/example
yuta:~/go-oidc/example (v3=) $ cat README.md
# Examples

These are example uses of the oidc package. Each requires a Google account and the client ID and secret of a registered OAuth2 application. To create one:

1. Visit your [Google Developer Console][google-developer-console].
2. Click "Credentials" on the left column.
3. Click the "Create credentials" button followed by "OAuth client ID".
4. Select "Web application" and add "http://127.0.0.1:5556/auth/google/callback" as an authorized redirect URI.
5. Click create and add the printed client ID and secret to your environment using the following variables:

Google Developer Consoleへアクセス

認証情報(アプリケーション)を作る

image.png

設定する

image.png

環境変数の設定

  • 作成すると、クライアントIDとクライアントシークレットが作られる
export GOOGLE_OAUTH2_CLIENT_ID=739786550065-g36863uform2efr5nrvvmjpj30pu9nuf.apps.googleusercontent.com
export GOOGLE_OAUTH2_CLIENT_SECRET=4sLRLGBdYu_un8C58c8Yg2yr

osパッケージで実行サーバのOS環境変数が読み込まれる

var (
  clientID     = os.Getenv("GOOGLE_OAUTH2_CLIENT_ID")
  clientSecret = os.Getenv("GOOGLE_OAUTH2_CLIENT_SECRET")
)

動作確認

起動

yuta:~/go-oidc/example (v3=) $ go run idtoken/app.go
2021/01/17 16:13:04 listening on http://127.0.0.1:5556/

http://127.0.0.1:5556/へアクセス

image.png

認証成功時

image.png

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?