2016-07-14 13 views
4
petstore_auth: 
    type: oauth2 
    authorizationUrl: http://swagger.io/api/oauth/dialog 
    flow: implicit 
    scopes: 
    write:pets: modify pets in your account 
    read:pets: read your pets 

これはthe Swagger SpecificationのsecurityDefinitionsの例です。 は何を書いていますか:ペットおよび読書:ptesは?パスにはいくつかのカテゴリがありますか?スワッガーセキュリティスキームオブジェクトの「スコープ」フィールドとは何ですか?

答えて

8

書き込み:ペット読む:ペットOauth2 scopesであり、OpenAPIの(FKA闊歩。)operations categorizationとは関係ありません。 APIは、のOauthで固定されている場合

のOAuth2が

をスコープ、スコープは異なる権利/ APIの消費者に権限を与えるために使用されています。スコープは名前で定義されます(必要なものを使用できます)。

APIの消費者としての役割を果たすことができ SwaggerUI

のOAuthスコープの承認:

  • 読む:ペット:こののOAuth2セキュアなAPIは2つのスコープを提案する。この場合 enter image description here

    アカウント

  • でペットを変更
  • 書き込み:ペット:ペットを読む

OpでAPIを記述する場合enAPI(fka。 Swagger)仕様では、質問に示すようにこれらのスコープを定義できます。

ただし、これらのスコープでどの操作が宣言されていない場合でも、これらのスコープの定義のみが役に立ちません。これは、操作にこれを追加することによって行われる :この例では

 security: 
     - petstore_auth: 
      - read:pets 

、操作は彼がread:petsスコープを使用することを許可された場合にのみ、APIの消費者にアクセス可能です。 1つの操作は複数のoauth2スコープに属することができ、複数のセキュリティ定義にも属することに注意してください。ここ

あなたがOpenAPIの(FKA。SWAGGER)のセキュリティについての詳細を読むことができます

OpenAPIの(FKA。闊歩)、動作分類

あなたはAPIの操作を分類する必要がある場合にかかわらずのOAuth2スコープの、あなたはtagsを使用することができます。

 tags: 
     - pets 

それはカテゴリpetsに置かれる操作にこれを追加することによって。 1回の操作で複数のカテゴリに属する​​ことができます。

これらのカテゴリは、SwaggerUIが操作を再編成するために使用します。

:あなたがここにカテゴリの詳細を読むことができ enter image description here :以下のスクリーンキャプチャでは、我々は3つのカテゴリー(ペット、店舗とユーザーを)見ることができます

Oauth2スコープとカテゴリを使用した完全な例は次のとおりです。

swagger: "2.0" 
info: 
    version: "1.0.0" 
    title: Swagger Petstore 

securityDefinitions: 
    petstore_auth: 
    type: oauth2 
    authorizationUrl: http://petstore.swagger.io/api/oauth/dialog 
    flow: implicit 
    scopes: 
     write:pets: modify pets in your account 
     read:pets: read your pets 

paths: 
    /pets/{petId}: 
    parameters: 
     - in: path 
     name: petId 
     description: ID of pet that needs to be fetched 
     required: true 
     type: integer 
     format: int64 
    get: 
     tags: 
     - pets 
     summary: Find pet by ID 
     responses: 
     "404": 
      description: Pet not found 
     "200": 
      description: A pet 
      schema: 
      $ref: "#/definitions/Pet" 
     security: 
     - petstore_auth: 
      - read:pets 
    delete: 
     tags: 
     - pets 
     summary: Deletes a pet 
     responses: 
     "404": 
      description: Pet not found 
     "204": 
      description: Pet deleted 
     security: 
     - petstore_auth: 
      - write:pets 

definitions: 
    Pet: 
    type: object 
    properties: 
     id: 
     type: integer 
     format: int64 
     name: 
     type: string 
     example: doggie 
+0

ありがとう、@Arnaud!ところで、私はすでにあなたの "writing-openapi-swagger-specification-tutorial"シリーズを読む予定でした! :) – lfree

関連する問題