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?

More than 1 year has passed since last update.

Post manのレスポンスで401が返ってきた。

Last updated at Posted at 2023-07-22

はじめに

  • 社内での開発中(open api 3.0を使って新規APIの作成みたいな作業)の話。
  • open api を定義し、コード生成し、postmanでリクエストを投げたら、401レスポンスが返ってきた。

401 Unauthorized とは

401 Unauthorizedは適切な認証情報を与えずリクエストを行った状態。

以下は、書籍「webを支える技術」から引用しています。

# リクエスト
POST /test HTTP/1.1
Host: exaample.jp

# レスポンス
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Example.jp"

401レスポンスの原因

  • Post manのHeaderに認証キーを入れなかったため。
  • 元々の定義でbearer認証が設定されており、tokenを入れたら行けた。

open api 3.0に認証に関して

  • open api 3.0には主に以下に三種類の認証を設定できる
    • Basic Authentication 
    • API Keys
    • Bearer Authentication(also called token authentication)
  • ちなみに「bearer」は「持ってくる」的な意味があり、仕様書にもそんなニュアンスで書いてある→The name “Bearer authentication” can be understood as “give access to the bearer of this token.”

引用:swagger

定義方法、認証方法

  • opan api の定義方法
openapi: 3.0.0
...
# 1) Define the security scheme type (HTTP bearer)
components:
  securitySchemes:
    bearerAuth:            # arbitrary name for the security scheme
      type: http
      scheme: bearer
      bearerFormat: JWT    # optional, arbitrary value for documentation purposes
# 2) Apply the security globally to all operations
security:
  - bearerAuth: []         # use the same name as above
  • post manではHeadersのKEYにAuthorization、VALUEにBearer <token>を記述する。

その他

  • 記事を書きながら「 realm」という単語が気になったので調べてみた。
    • realm
      • 訳:領域・範囲・分野
      • 原義:「王国(の領土)」「領地」など君主の支配が及ぶ範囲。
    • IT用語のrealm
      • 認証情報、アクセス権、セキュリティポリシーなどが適用される範囲
      • 使い方→「利用者はシステムにログインすると、当該レルム内にある機器やサービスにアクセスできる。」

参考:レルム

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?