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

Cognitoで登録時のユーザー名の予約語を作成

Last updated at Posted at 2024-06-04

ユーザー名に www とか official とか、認めたくない

Cognitoを使ったユーザー管理において、登録時に上記のようなワードを禁止しておきたいということがあります。
例えば、Twitter(X)では http://x.com/<ユーザー名> でその人のプロフィールが表示されます。こういった実装を考えずとも、admin や official など、他のユーザーを惑わすようなIDはあらかじめ弾きたいものです。

そのためのメソッドが公式に記載されていました。

やり方ですが、Lambdaを一つ作り、Cognitoコンソール画面から登録していくだけです。またそのためのライブラリが用意されているので使用します。

以下、すでにユーザープールの設定等は済んでいる前提で進めていきます。

Cognitoのユーザープールの設定タブに「ユーザープールのプロパティ」があります。
その中には「Lambda トリガー」があります。Cognitoでの登録時や、登録後に、ここで登録したLambdaを起動することができます。今回のように登録時にユーザー名を検証する他に、登録後にその情報の一部を使ってDynamoDBへデータを送付するであるとか、色々と便利に使える機能です。

最初に、ここに登録するLambdaを作ります。

username_validatorライブラリを使う必要がありますので準備していきましょう。ローカルでpython3が動くとして、適当なワーキングディレクトリを作り、その中で

pip3 install username-validator -t .

とします。同じフォルダに lambda_function.py を作成し、

lambda_function.py
from username_validator import UsernameValidator

MY_RESERVED = [
  "larry",
  "aws",
  "reinvent"
]

validator = UsernameValidator(additional_names=MY_RESERVED)

def lambda_handler(event, context):
  user = event['userName']

  if not user.islower():
    raise Exception("Username must be lowercase")

  validator.validate_all(user)

  return event

としておきます。
MY_RESERVEDの中身は好きに書き換えてください。ライブラリであらかじめ予約されている単語は以下の通りです。(ライブラリより貼り付け)

SPECIAL_HOSTNAMES = [
    # Hostnames with special/reserved meaning.
    'autoconfig',     # Thunderbird autoconfig
    'autodiscover',   # MS Outlook/Exchange autoconfig
    'broadcasthost',  # Network broadcast hostname
    'isatap',         # IPv6 tunnel autodiscovery
    'localdomain',    # Loopback
    'localhost',      # Loopback
    'wpad',           # Proxy autodiscovery
]


PROTOCOL_HOSTNAMES = [
    # Common protocol hostnames.
    'ftp',
    'imap',
    'mail',
    'news',
    'pop',
    'pop3',
    'smtp',
    'usenet',
    'uucp',
    'webmail',
    'www',
]


CA_ADDRESSES = [
    # Email addresses known used by certificate authorities during
    # verification.
    'admin',
    'administrator',
    'hostmaster',
    'info',
    'is',
    'it',
    'mis',
    'postmaster',
    'root',
    'ssladmin',
    'ssladministrator',
    'sslwebmaster',
    'sysadmin',
    'webmaster',
]


RFC_2142 = [
    # RFC-2142-defined names not already covered.
    'abuse',
    'marketing',
    'noc',
    'sales',
    'security',
    'support',
]


NOREPLY_ADDRESSES = [
    # Common no-reply email addresses.
    'mailer-daemon',
    'nobody',
    'noreply',
    'no-reply',
]


SENSITIVE_FILENAMES = [
    # Sensitive filenames.
    'clientaccesspolicy.xml',  # Silverlight cross-domain policy file.
    'crossdomain.xml',         # Flash cross-domain policy file.
    'favicon.ico',
    'humans.txt',
    'keybase.txt',  # Keybase ownership-verification URL.
    'robots.txt',
    '.htaccess',
    '.htpasswd',
]


OTHER_SENSITIVE_NAMES = [
    # Other names which could be problems depending on URL/subdomain
    # structure.
    'account',
    'accounts',
    'blog',
    'buy',
    'clients',
    'contact',
    'contactus',
    'contact-us',
    'copyright',
    'dashboard',
    'doc',
    'docs',
    'download',
    'downloads',
    'enquiry',
    'faq',
    'help',
    'inquiry',
    'license',
    'login',
    'logout',
    'me',
    'myaccount',
    'payments',
    'plans',
    'portfolio',
    'preferences',
    'pricing',
    'privacy',
    'profile',
    'register'
    'secure',
    'settings',
    'signin',
    'signup',
    'ssl',
    'status',
    'subscribe',
    'terms',
    'tos',
    'user',
    'users'
    'weblog',
    'work',
]

zipにします。

 zip -r upload.zip *  

次に、Lambdaを「username-validator」などの名前で立ち上げてください。

Lambdaの設定ですが、ランタイムは3.10前後ならなんでもよく、追加のIAMロールも不要かと思います。必要ならCloudWatchへの書き込み権限などを与えると良いかもしれません。

作成したzipをアップロードします。

最後に、Lambdaの設定が終わったら、CognitoでLamdbaを登録してください。サインアップ時に起動します。

スクリーンショット 2024-06-04 19.07.15.png

これで完了です。

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