ホーム 記事一覧 ブック DH週間トピックス 検索 このサイトについて
English
NextAuth.jsを使って、ORCID・The Open Science Framework・ GakuNin RDMの認証を行う

NextAuth.jsを使って、ORCID・The Open Science Framework・ GakuNin RDMの認証を行う

概要 NextAuth.jsを使って、ORCID・OSF(The Open Science Framework)・ GRDM(GakuNin RDM)の認証を行う方法です。 デモアプリ ORCID https://orcid-app.vercel.app/ OSF https://osf-app.vercel.app/ GRDM https://rdm-app.vercel.app/ リポジトリ ORCID https://github.com/nakamura196/orcid_app 以下がオプションの記述例です。 https://github.com/nakamura196/orcid_app/blob/main/src/app/api/auth/[…nextauth]/authOptions.js export const authOptions = { providers: [ { id: "orcid", name: "ORCID", type: "oauth", clientId: process.env.ORCID_CLIENT_ID, clientSecret: process.env.ORCID_CLIENT_SECRET, authorization: { url: "https://orcid.org/oauth/authorize", params: { scope: "/authenticate", response_type: "code", redirect_uri: process.env.NEXTAUTH_URL + "/api/auth/callback/orcid", }, }, token: "https://orcid.org/oauth/token", userinfo: { url: "https://pub.orcid.org/v3.0/[ORCID]", async request({ tokens }) { const res = await fetch(`https://pub.orcid.org/v3.0/${tokens.orcid}`, { headers: { Authorization: `Bearer ${tokens.access_token}`, Accept: "application/json", }, }); return await res.json(); }, }, profile(profile) { return { id: profile["orcid-identifier"].path, // ORCID の ID を取得 name: profile.person?.name?.["given-names"]?.value + " " + profile.person?.name?.["family-name"]?.value, email: profile.person?.emails?.email?.[0]?.email, }; }, }, ], callbacks: { async session({ session, token }) { session.accessToken = token.accessToken; session.user.id = token.orcid; // ORCID ID をセッションに追加 return session; }, async jwt({ token, account }) { if (account) { token.accessToken = account.access_token; token.orcid = account.orcid; } return token; }, }, }; OSF https://github.com/nakamura196/osf-app ...

DrupalのSimple OAuthとPostmanを使ったOAuth認証の確認

DrupalのSimple OAuthとPostmanを使ったOAuth認証の確認

概要 DrupalのSimple OAuthとPostmanを使ったOAuth認証の確認を行います。 以前に以下の記事を書きましたが、もう少し掘り下げてみます。 DrupalでSimple OAuthの設定を行う 以下を参考にしてください。 https://tech.ldas.jp/ja/posts/e4ce978db12227/#oauthクライアントの作成 Postman グラントタイプがpasswordの場合 /oauth/token に対して、Body > x-www-form-urlencoded に以下を指定しました。 キー 値 grant_type password client_id {作成したCLIENT_ID。例:gt8UKlKltI4qs1XP5KLucIXiYw9ulGb0xS4RyO437dc} client_secret {作成したCLIENT_SECRET。例:test} username {ユーザ名。例:yamato} password {パスワード。例:yamato} 結果、以下のようなJSONが返却されました。 { "token_type": "Bearer", "expires_in": 300, "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJS...", "refresh_token": "def50200295e412f..." } jwt.ioで確認したところ、以下のようにデコードされました。 { "aud": "gt8UKlK...", "jti": "6dc1fee..", "iat": 1709386974, "nbf": 1709386974, "exp": 1709387274.122002, "sub": "2", "scope": [ "authenticated", "cj" ] } subはDrupalのユーザのIDに該当し、scopeはDrupalで設定した値が返却されました。 異なるユーザでログインした場合、異なるsubが与えられました。 ユーザ名またはパスワードをまちがえる 以下が返却されました。 { "error": "invalid_grant", "error_description": "The user credentials were incorrect.", "message": "The user credentials were incorrect." } 間違ったscopeを指定する 以下のように、間違ったscopeを指定します。 キー 値 grant_type password client_id {作成したCLIENT_ID。例:gt8UKlKltI4qs1XP5KLucIXiYw9ulGb0xS4RyO437dc} client_secret {作成したCLIENT_SECRET。例:test} username {ユーザ名。例:yamato} password {パスワード。例:yamato} scope test 以下が返却されました。 ...

Drupalのsimple_oauthモジュールを用いたRESTリソースのoauth2認証を試す

Drupalのsimple_oauthモジュールを用いたRESTリソースのoauth2認証を試す

概要 DrupalのSimple OAuth (OAuth2) & OpenID ConnectモジュールはOAuth 2.0認証フレームワークRFCの実装と説明されています。 https://www.drupal.org/project/simple_oauth 関連する記事として、cookie認証の例や、jwt認証の例も参考にしてください。 インストール simple_oauthモジュールには、5系と6系があるようですが、今回は5系を使用します。以下でインストールします。 composer.phar require 'drupal/simple_oauth:^5.2' ただし、さくらレンタルサーバを使用している場合、以下のエラーが発生しました。PHP's sodium extensionが必要でした。 composer.phar require 'drupal/simple_oauth:^5.2' ./composer.json has been updated Running composer update drupal/simple_oauth Loading composer repositories with package information Updating dependencies Your requirements could not be resolved to an installable set of packages. Problem 1 - steverhoades/oauth2-openid-connect-server v2.4.0 requires lcobucci/jwt 4.1.5 -> satisfiable by lcobucci/jwt[4.1.5]. - steverhoades/oauth2-openid-connect-server[v2.6.0, ..., v2.6.1] require lcobucci/jwt 4.1.5|^4.2|^4.3|^5.0 -> satisfiable by lcobucci/jwt[4.1.5, ..., 4.4.x-dev, 5.0.0, ..., 5.3.x-dev]. - steverhoades/oauth2-openid-connect-server v2.5.0 requires lcobucci/jwt 4.1.5|^4.2 -> satisfiable by lcobucci/jwt[4.1.5, ..., 4.4.x-dev]. - drupal/simple_oauth[5.2.0, ..., 5.x-dev] require drupal/core ^8 || ^9 -> found drupal/core[8.0.0-beta6, ..., 8.9.x-dev, 9.0.0-alpha1, ..., 9.5.x-dev] but the package is fixed to 10.2.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command. - lcobucci/jwt[4.1.5, ..., 4.4.x-dev, 5.0.0, ..., 5.3.x-dev] require ext-sodium * -> it is missing from your system. Install or enable PHP's sodium extension. - drupal/simple_oauth[5.2.3, ..., 5.2.x-dev] require steverhoades/oauth2-openid-connect-server ^2.4 -> satisfiable by steverhoades/oauth2-openid-connect-server[v2.4.0, v2.5.0, v2.6.0, v2.6.1]. - Root composer.json requires drupal/simple_oauth ^5.2 -> satisfiable by drupal/simple_oauth[5.2.0, ..., 5.x-dev]. To enable extensions, verify that they are enabled in your .ini files: - /usr/local/php/8.1/etc/php.ini - /usr/local/php/8.1/etc/conf.d/apcu.ini - /usr/local/php/8.1/etc/conf.d/imagick.ini - /usr/local/php/8.1/etc/conf.d/mcrypt.ini - /usr/local/php/8.1/etc/conf.d/opcache.ini You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode. Alternatively, you can run Composer with `--ignore-platform-req=ext-sodium` to temporarily ignore these required extensions. Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions. Installation failed, reverting ./composer.json and ./composer.lock to their original content. そこで、以下のサイトを参考に、PHP-sodium 拡張を追加しました。 ...

Auth0 プロバイダーと Strapi のチュートリアル

Auth0 プロバイダーと Strapi のチュートリアル

概要 Auth0 を使用して Strapi を認証する方法を試しました。以下の記事の通りに進めることで、実現することができました。 https://strapi.io/blog/auth0-provider-and-strapi-tutorial-1 ただし、Allowed Callback URLsを指定する箇所で、http://localhost:1337/connect/auth0/callbackとなっている箇所を、http://localhost:1337/api/connect/auth0/callbackに修正する必要がありました。 (参考)github 上記手順を参考に、githubをプロパイダーとして使用する方法もうまく設定できました。