SAP mobile service にcurlだけで認証を行うチャレンジ

curlだけで認証できるか試す

現状:
curlを使用して、レスポンスからauthorization_codeを取得する方法がわからない。

token の取得

スクリーンショット 2020-05-14 10.36.06.png

AOuth client credentials の取得

https://blogs.sap.com/2018/07/29/oauth-client-grant-types-authorization_code-password/
https://blogs.sap.com/2018/01/10/secured-access-to-client-applications-using-oauth-2.0/

tokenを取得するためにリクエストを投げたいけど、url、パラメーター、ヘッダーはなに?

スクリーンショット 2020-05-14 11.26.06.png

postman使って取得

スクリーンショット 2020-05-14 12.59.35.png

# 最初にアクセスされる場所
https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/oauth2/api/v1/authorize?response_type=code&state=&client_id=690ec568-46b8-42d6-af95-ec45ce23f2ac&scope=&redirect_uri=https%3A%2F%2Foauth.pstmn.io%2Fv1%2Fcallback

# Cookieを削除しないとリダイレクトされる場所
https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/?code=GJSXjNJLUD

# クッキーを削除した場合にリダイレクトされる場所 Un
https://accounts.sap.com/saml2/idp/sso?sp=uaa-cf-eu10&RelayState=client_id%3D45be646d-0b43-4c0c-a44a-3fa237b59fe3%26response_type%3Dcode%26redirect_uri%3Dhttps%253A%252F%252Fhostl.authentication.eu10.hana.ondemand.com%252Flogin%252Fcallback%252Fsap.default%26state%3DSt2uGMarG8%26scope%3Dopenid%26nonce%3DDEbRbtC8aszO

Postmanでtoken取得

ID passwordを入力して

スクリーンショット 2020-05-14 13.16.09.png

authorization_codeが最後にあるのでこちらを使用してリクエスト投げる。
スクリーンショット 2020-05-14 15.13.41.png

スクリーンショット 2020-05-14 15.14.36.png

まずはコマンドラインから

ヘッダーなし → Unauthorized
ヘッダーにベース認証 → Unauthorized

$ curl -X POST -I -L --trace-ascii - --header 'Authorization: Basic c2hpbnRhcm9AamFzbWluZS1zLmNvbTpLInRzODgxMTAz'  "https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/oauth2/api/v1/authorize?response_type=code&state=&client_id=690ec568-46b8-42d6-af95-ec45ce23f2ac&scope=&redirect_uri=https%3A%2F%2Foauth.pstmn.io%2Fv1%2Fcallback"
== Info:   Trying 3.124.208.223...
== Info: TCP_NODELAY set
== Info: Connected to host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com (3.124.208.223) port 443 (#0)
0163: 
<= Recv header, 27 bytes (0x1b)
0000: HTTP/1.1 401 Unauthorized
HTTP/1.1 401 Unauthorized
<= Recv header, 52 bytes (0x34)
0000: Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: no-cache, no-store, must-revalidate
<= Recv header, 20 bytes (0x14)
0000: Content-Length: 12
Content-Length: 12
<= Recv header, 37 bytes (0x25)
0000: Date: Thu, 14 May 2020 04:13:10 GMT
Date: Thu, 14 May 2020 04:13:10 GMT
<= Recv header, 29 bytes (0x1d)
0000: X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
<= Recv header, 64 bytes (0x40)
0000: X-Smp-Log-Correlation-Id: 1ff8c647-2c69-4dc2-7d1c-7a53ea908cc6
X-Smp-Log-Correlation-Id: 1ff8c647-2c69-4dc2-7d1c-7a53ea908cc6
<= Recv header, 57 bytes (0x39)
0000: X-Vcap-Request-Id: 1ff8c647-2c69-4dc2-7d1c-7a53ea908cc6
X-Vcap-Request-Id: 1ff8c647-2c69-4dc2-7d1c-7a53ea908cc6
<= Recv header, 74 bytes (0x4a)
0000: Strict-Transport-Security: max-age=31536000; includeSubDomains; 
0040: preload;
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload;

<= Recv header, 2 bytes (0x2)
0000: 
== Info: Excess found in a non pipelined read: excess = 12 url = /oauth2/api/v1/authorize (zero-length body)
== Info: Connection #0 to host host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com left intact
* Closing connection 0
$ curl --location --request POST 'https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/oauth2/api/v1/token?client_id=690ec568-46b8-42d6-af95-ec45ce23f2ac&response_type=code&redirect_uri=https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com&grant_type=authorization_code&code=nfzsmSWcT5'

authorization_code を使用してtoken取得

https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/oauth2/api/v1/token?client_id=690ec568-46b8-42d6-af95-ec45ce23f2ac&response_type=code&redirect_uri=https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com&grant_type=authorization_code&code=wPs4HtirMZ
{
"access_token":"<token>",
"refresh_token":"<refresh_token>",
"expires_in":43199,
"scope":"openid user_attributes uaa.user",
"jti":"<>"
}

やっと取得できた。。。

curlでauthorization_codeを取得する

$ curl -D -X POST --header 'Authorization: Basic c2hpbnRhcm9AamFzbWluZS1zLmNvbTpLInRzODgxMTAz'  "https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/oauth2/api/v1/authorize?response_type=code&state=&client_id=690ec568-46b8-42d6-af95-ec45ce23f2ac&scope=&redirect_uri=https%3A%2F%2Foauth.pstmn.io%2Fv1%2Fcallback"
Warning: The file name argument '-X' looks like a flag.
curl: (6) Could not resolve host: POST
<html><head><link rel="shortcut icon" href="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><script>document.cookie="fragmentAfterLogin="+encodeURIComponent(location.hash)+";path=/";document.cookie="locationAfterLogin="+encodeURIComponent(location.href.split('#')[0].split(location.host)[1])+";path=/";document.cookie="signature=QLckM%2F3og2%2F%2FqKyY4utgmY%2F3TiQ%3D;path=/";location="https://host.authentication.eu10.hana.ondemand.com/oauth/authorize?response_type=code&client_id=sb-com-sample3-demo-dev!t46040&redirect_uri=https%3A%2F%host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com%2Flogin%2Fcallback"</script></head></html>

うーん、、ちょっとできない。
一旦保留

取得したtokenを使って、データにアクセスしてみる

$ curl -D --location --request GET 'https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/mobileservices/application/com.sample3.demo/endpoints/v1/runtime/application/com.sample3.demo/endpoints'  --header 'Authorization: Bearer <token>'

resopnse

[
    {
        "name": "com.sap.edm.sampleservice.v2",
        "url": "https://host-dev-com-sample3-demo.cfapps.eu10.hana.ondemand.com/com.sap.edm.sampleservice.v2",
        "path": "/com.sap.edm.sampleservice.v2",
        "ssoMethod": "FORWARD"
    }
]

疎通はできていそう。

課題

  • authorization_codeの取得方法
    • 現在ログインページにリダイレクトされて、ID passwordを入力後、表示されるurlのパラメータに表示されているものを使用している。コマンドラインから取得できる方法がわからない。

コメントを残す

%d人のブロガーが「いいね」をつけました。