# curl
cURLでブラウザから特定のサイトにログインする動作を再現する。
Titech App Project課題1の自分用振り返り
## references
1. [Link userAgent](https://travelingresearcher.com/entry/2017/04/30/115255)
2. [Link cookie](https://ryota-ka.hatenablog.com/entry/2017/03/23/013749)
3. [Link cURL形式copy](https://qiita.com/ueokande/items/a580e9d9f17dbf82f382)
4. [Link cURL POST examples](https://gist.github.com/subfuzion/08c5d85437d5d4f00e58)

上のようにして`cURL`を取得できる。
最終的な成果物
```shell=
#!/bin/zsh
read usr_name
read usr_password
curl 'https://portal.nap.gsic.titech.ac.jp/GetAccess/Login?Template=userpass_key&AUTHMETHOD=UserPassword' \
-H 'Connection: keep-alive' \
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: https://portal.nap.gsic.titech.ac.jp/' \
-H 'Accept-Language: ja,en-US;q=0.9,en;q=0.8' \
-c cookie \
--compressed > out.html
cat out.html | grep CSRFFormToken > csrf
CSRF=`cat csrf | sed -e "s/^.*\(value=\'.*\'\).*$/\1/" | cut -c 8- | rev | cut -c 2- | rev `
pageGenTime=`cat out.html | grep pageGenTime | sed -e "s/^.*\(value=\".*\"\).*$/\1/" | cut -c 8- | rev | cut -c 2- | rev`
Cookie=`cat cookie | grep JSESSIONID | sed -e "s/^.*\(JSESSIONID.*\)$/\1/" | cut -c 12- `
echo $CSRF
echo $pageGenTime
echo $Cookie
curl -L 'https://portal.nap.gsic.titech.ac.jp/GetAccess/Login' \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' \
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Origin: https://portal.nap.gsic.titech.ac.jp' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: https://portal.nap.gsic.titech.ac.jp/GetAccess/Login?Template=userpass_key&AUTHMETHOD=UserPassword' \
-H 'Accept-Language: ja,en-US;q=0.9,en;q=0.8' \
-H 'Cookie: JSESSIONID='$Cookie';' \
-c cookie \
--data-raw 'usr_name='$usr_name'&OK=++++OK++++&AUTHTYPE=&HiddenURI=https%3A%2F%2Fportal.nap.gsic.titech.ac.jp%2FGetAccess%2FResourceList&Template=userpass_key&AUTHMETHOD=UserPassword&pageGenTime='$pageGenTime'&LOCALE=ja_JP&CSRFFormToken='$CSRF \
--data usr_password=$usr_password \
--compressed
AUTH_SESSION_ID=`cat cookie | grep AUTH_SESSION_ID | sed -e "s/^.*\(JSESSIONID.*\)$/\1/" | cut -c 56- `
echo $AUTH_SESSION_ID
curl 'https://portal.nap.gsic.titech.ac.jp/GetAccess/Login?Template=idg_key&AUTHMETHOD=IG&GASF=CERTIFICATE,IG.GRID,IG.TOKENRO,IG.OTP&LOCALE=ja_JP&GAREASONCODE=13&GAIDENTIFICATIONID=UserPassword&GARESOURCEID=resourcelistID2&GAURI=https://portal.nap.gsic.titech.ac.jp/GetAccess/ResourceList&Reason=13&APPID=resourcelistID2&URI=https://portal.nap.gsic.titech.ac.jp/GetAccess/ResourceList' \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'Referer: https://portal.nap.gsic.titech.ac.jp/GetAccess/Login?Template=userpass_key&AUTHMETHOD=UserPassword' \
-H 'Accept-Language: ja,en-US;q=0.9,en;q=0.8' \
-H 'Cookie: JSESSIONID='$Cookie'; AUTH_SESSION_ID='$AUTH_SESSION_ID \
--compressed > out2.html
echo $usr_name
echo $usr_password
exit 0
```