SlideShare a Scribd company logo
© Hitachi, Ltd. 2020. All rights reserved.
OSSセキュリティ技術の会 第八回勉強会
株式会社 日立製作所
OSSソリューションセンタ
2020/06/05
茂木 昂士
OAuth 入門
- Resource Serverのつくり方 -
© Hitachi, Ltd. 2020. All rights reserved.
自己紹介
1
- 茂木 昂士(もぎ たかし)
- 所属 : 日立製作所 OSSソリューションセンタ
- 業務 : APIセキュリティに関連するOSSの調査・検証
- @IT連載
- Keycloak超入門
- http://www.atmarkit.co.jp/ait/series/7363/
© Hitachi, Ltd. 2020. All rights reserved.
1. APIとセキュリティ
2
© Hitachi, Ltd. 2020. All rights reserved.
OAuth の基本
3
Authorization Server, Client, Resource Server
Client
Authorization Server
Resource Server
今日はこの部分の説明
© Hitachi, Ltd. 2020. All rights reserved.
Resource Server のセキュリティ
4
リクエストを受け取ったResource Serverは何をするのか?
- Access Tokenの有効性
- iss, exp, aud etc…
- End Userの属性
- ID, Role, Group などユーザに��づく属性値を確認
- Scope
- Client がアクセスを許可された範囲
上記を確認してリクエスト要否を決定する
© Hitachi, Ltd. 2020. All rights reserved.
OAuth を利用する際の API デザイン
5
一般的なAPI
/users/{userID}/messages
OAuthのAPI
/me/messages
ユーザ属性はAccess Tokenに紐づいている
- User IDなどのユーザ属性は Access Token から取得する
- Path Parameterと Access Token 両方をチェックする
- 煩雑、バグの温床になりうる
- API GWなどで変換してしまうのもあり
© Hitachi, Ltd. 2020. All rights reserved.
2. Access Token の検証方法
6
© Hitachi, Ltd. 2020. All rights reserved.
Access Tokenの種類
7
Access Tokenの表現方法から2種類に分けられる
- Handle
- ユーザ属性情報への参照
- Reference Token, Opaque Token などと呼ばれる
- Assertion
- ユーザ属性を内包する
- Self-Contained Tokenとも呼ばれる
- 一般的にはJSON Web Token (JWT)が利用される
RFC 6819 OAuth 2.0 Threat Model and Security Considerations
3.1 Tokens より
© Hitachi, Ltd. 2020. All rights reserved.
Handle Token の検証
8
RFC 6772 OAuth 2.0 Token Introspection
- Authorization Server が持つToken確認用のEndpoint
- Access Token の有効性確認、ユーザ属性の取得
HTTP/1.1 200 OK
Content-Type: application/json
{
"active": true,
"client_id": "l238j323ds-23ij4",
"username": "jdoe",
"scope": "read write dolphin",
"sub": "Z5O3upPC88QrAjx00dis",
"aud": "https://protected.example.net/resource",
"iss": "https://server.example.com/",
"exp": 1419356238,
"iat": 1419350238,
"extension_field": "twenty-seven"
}
“active”: true なら有効
false でも 200 OK
© Hitachi, Ltd. 2020. All rights reserved.
Assertion Token の検証
9
一般的に JWT (正確には JWS) が利用される
- iss や exp, aud などのClaimをチェック
- sub などからユーザのID取得
- 決まった形式はなく、ベンダー固有
JSON Web Token (JWT) Profile for OAuth 2.0 Access Tokens1
- Access Token の JWT を共通化しようとする取り組み
- 初版は2019/4 現在 Draft 7
[1]: http://tools.ietf.org/html/draft-ietf-oauth-access-token-jwt-07
© Hitachi, Ltd. 2020. All rights reserved.
Token Introspection vs JWT
10
Token Introspection
- Authorization Server への負荷 増
- Handle, Assertion どちらでも 可
JWT
- スケールしやすい
- 期限前の無効化が難しい
- プライバシーの問題
要件によって使い分け、組み合わせも可能
© Hitachi, Ltd. 2020. All rights reserved.
3. Spring Security Resource Server
11
© Hitachi, Ltd. 2020. All rights reserved.
Spring Security OAuth2 Resource Server
12
Spring Security の機能のひとつ
- OAuth Resource Server を実現するためのライブラリ
- Handle, Assertion (JWT) 両方に対応
- 5.1 から JWT に対応
- Opaque (Handle) Token には 5.2.1 から
- Qiitaに 「Keycloak と Spring Security で Token Introspection1」 書いてます
- Authorization Server が別に必要
[1]: https://qiita.com/t-mogi/items/4a513ab80774f2c6521b
© Hitachi, Ltd. 2020. All rights reserved.
Spring Security と Spring Security OAuth
13
Spring Security OAuth は別のプロジェクト
- 現在 Deprecated
- OAuth 関連は Spring Security に統一された
- Resource Server, Client のみ
- Authorization Server の機能はなくなった
→ Spring Authorization Server として開発が始まる(4月)
検索しづらく、古い情報にあたることが多いので注意が必要
[1]: https://github.com/spring-projects-experimental/spring-authorization-server
© Hitachi, Ltd. 2020. All rights reserved.
Spring Security での 実装例
14
Token Introspection の URLを指定
spring:
security:
oauth2:
resourceserver:
opaquetoken:
introspection-uri: http://localhost:8080/…introspect
client-id: sample
client-secret: <client_secretの値>
@RequestMapping("/api/protected/")
@PreAuthorize("hasAuthority('SCOPE_read')")
public String index(JwtAuthenticationToken authenticationToken) {
Jwt token = authenticationToken.getToken();
String username = token.getClaimAsString("preferred_username");
…
}
Scopeでの制御、ユーザ属性の取得
© Hitachi, Ltd. 2020. All rights reserved.
株式会社 日立製作所
OSS ソリューションセンター
- Resource Serverのつくり方 -
OAuth 入門
2020/06/05
茂木 昂士
END
15
16© Hitachi, Ltd. 2020. All rights reserved.
他社所有商標に関する表示
• HITACHIは、株式会社 日立製作所の商標または登録商標です。
• Springは米国及びその他の国におけるPivotal Software, Inc. の登録商標です。
• その他記載の会社名、製品名などは、それぞれの会社の商標もしくは登録商標です。
OAuth 2.0のResource Serverの作り方

More Related Content

OAuth 2.0のResource Serverの作り方

  • 1. © Hitachi, Ltd. 2020. All rights reserved. OSSセキュリティ技術の会 第八回勉強会 株式会社 日立製作所 OSSソリューションセンタ 2020/06/05 茂木 昂士 OAuth 入門 - Resource Serverのつくり方 -
  • 2. © Hitachi, Ltd. 2020. All rights reserved. 自己紹介 1 - 茂木 昂士(もぎ たかし) - 所属 : 日立製作所 OSSソリューションセンタ - 業務 : APIセキュリティに関連するOSSの調査・検証 - @IT連載 - Keycloak超入門 - http://www.atmarkit.co.jp/ait/series/7363/
  • 3. © Hitachi, Ltd. 2020. All rights reserved. 1. APIとセキュリティ 2
  • 4. © Hitachi, Ltd. 2020. All rights reserved. OAuth の基本 3 Authorization Server, Client, Resource Server Client Authorization Server Resource Server 今日はこの部分の説明
  • 5. © Hitachi, Ltd. 2020. All rights reserved. Resource Server のセキュリティ 4 リクエストを受け取ったResource Serverは何をするのか? - Access Tokenの有効性 - iss, exp, aud etc… - End Userの属性 - ID, Role, Group などユーザに紐づく属性値を確認 - Scope - Client がアクセスを許可された範囲 上記を確認してリクエスト要否を決定する
  • 6. © Hitachi, Ltd. 2020. All rights reserved. OAuth を利用する際の API デザイン 5 一般的なAPI /users/{userID}/messages OAuthのAPI /me/messages ユーザ属性はAccess Tokenに紐づいている - User IDなどのユーザ属性は Access Token から取得する - Path Parameterと Access Token 両方をチェックする - 煩雑、バグの温床になりうる - API GWなどで変換してしまうのもあり
  • 7. © Hitachi, Ltd. 2020. All rights reserved. 2. Access Token の検証方法 6
  • 8. © Hitachi, Ltd. 2020. All rights reserved. Access Tokenの種類 7 Access Tokenの表現方法から2種類に分けられる - Handle - ユーザ属性情報への参照 - Reference Token, Opaque Token などと呼ばれる - Assertion - ユーザ属性を内包する - Self-Contained Tokenとも呼ばれる - 一般的にはJSON Web Token (JWT)が利用される RFC 6819 OAuth 2.0 Threat Model and Security Considerations 3.1 Tokens より
  • 9. © Hitachi, Ltd. 2020. All rights reserved. Handle Token の検証 8 RFC 6772 OAuth 2.0 Token Introspection - Authorization Server が持つToken確認用のEndpoint - Access Token の有効性確認、ユーザ属性の取得 HTTP/1.1 200 OK Content-Type: application/json { "active": true, "client_id": "l238j323ds-23ij4", "username": "jdoe", "scope": "read write dolphin", "sub": "Z5O3upPC88QrAjx00dis", "aud": "https://protected.example.net/resource", "iss": "https://server.example.com/", "exp": 1419356238, "iat": 1419350238, "extension_field": "twenty-seven" } “active”: true なら有効 false でも 200 OK
  • 10. © Hitachi, Ltd. 2020. All rights reserved. Assertion Token の検証 9 一般的に JWT (正確には JWS) が利用される - iss や exp, aud などのClaimをチェック - sub などからユーザのID取得 - 決まった形式はなく、ベンダー固有 JSON Web Token (JWT) Profile for OAuth 2.0 Access Tokens1 - Access Token の JWT を共通化しようとする取り組み - 初版は2019/4 現在 Draft 7 [1]: http://tools.ietf.org/html/draft-ietf-oauth-access-token-jwt-07
  • 11. © Hitachi, Ltd. 2020. All rights reserved. Token Introspection vs JWT 10 Token Introspection - Authorization Server への負荷 増 - Handle, Assertion どちらでも 可 JWT - スケールしやすい - 期限前の無効化が難しい - プライバシーの問題 要件によって使い分け、組み合わせも可能
  • 12. © Hitachi, Ltd. 2020. All rights reserved. 3. Spring Security Resource Server 11
  • 13. © Hitachi, Ltd. 2020. All rights reserved. Spring Security OAuth2 Resource Server 12 Spring Security の機能のひとつ - OAuth Resource Server を実現するためのライブラリ - Handle, Assertion (JWT) 両方に対応 - 5.1 から JWT に対応 - Opaque (Handle) Token には 5.2.1 から - Qiitaに 「Keycloak と Spring Security で Token Introspection1」 書いてます - Authorization Server が別に必要 [1]: https://qiita.com/t-mogi/items/4a513ab80774f2c6521b
  • 14. © Hitachi, Ltd. 2020. All rights reserved. Spring Security と Spring Security OAuth 13 Spring Security OAuth は別のプロジェクト - 現在 Deprecated - OAuth 関連は Spring Security に統一された - Resource Server, Client のみ - Authorization Server の機能はなくなった → Spring Authorization Server として開発が始まる(4月) 検索しづらく、古い情報にあたることが多いので注意が必要 [1]: https://github.com/spring-projects-experimental/spring-authorization-server
  • 15. © Hitachi, Ltd. 2020. All rights reserved. Spring Security での 実装例 14 Token Introspection の URLを指定 spring: security: oauth2: resourceserver: opaquetoken: introspection-uri: http://localhost:8080/…introspect client-id: sample client-secret: <client_secretの値> @RequestMapping("/api/protected/") @PreAuthorize("hasAuthority('SCOPE_read')") public String index(JwtAuthenticationToken authenticationToken) { Jwt token = authenticationToken.getToken(); String username = token.getClaimAsString("preferred_username"); … } Scopeでの制御、ユーザ属性の取得
  • 16. © Hitachi, Ltd. 2020. All rights reserved. 株式会社 日立製作所 OSS ソリューションセンター - Resource Serverのつくり方 - OAuth 入門 2020/06/05 茂木 昂士 END 15
  • 17. 16© Hitachi, Ltd. 2020. All rights reserved. 他社所有商標に関する表示 • HITACHIは、株式会社 日立製作所の商標または登録商標です。 • Springは米国及びその他の国におけるPivotal Software, Inc. の登録商標です。 • その他記載の会社名、製品名などは、それぞれの会社の商標もしくは登録商標です。