1
|
#!/bin/bash
|
2
|
|
3
|
SERVER="http://localhost:8080"
|
4
|
CLIENT="http://foo.bar/dataportal/"
|
5
|
|
6
|
|
7
|
echo "Testing OAuth2 at server "$SERVER
|
8
|
echo "================================================="
|
9
|
|
10
|
client_redirect_uri=$CLIENT'classification.php'
|
11
|
|
12
|
# authentication of the client if not trusted
|
13
|
client_secrets='-u any-client:secret'
|
14
|
# trusted client (is not working so far!)
|
15
|
#client_secrets=''
|
16
|
|
17
|
CURL=$(which curl)
|
18
|
|
19
|
|
20
|
# ================================================================================
|
21
|
|
22
|
echo "2) Authorization Code Grant"
|
23
|
|
24
|
echo " > oauth/authorize ..."
|
25
|
location=$(curl -v -u admin:00000 -v $SERVER/oauth/authorize\?response_type\=code\&client_id\=any-client\&redirect_uri\=$client_redirect_uri 2>&1 | sed -n 's/< Location: //p')
|
26
|
# remove trailing line break!
|
27
|
location_tmp=${location%$'\r'}
|
28
|
|
29
|
|
30
|
echo ' < OAuth2-Response: '$location_tmp
|
31
|
location=$(cut -d "?" -f1 <<< $location_tmp)
|
32
|
code=$(cut -d "?" -f2 <<< $location_tmp)
|
33
|
echo ' > oauth/token'
|
34
|
json_response=$(curl -s $client_secrets --data 'grant_type=authorization_code&'$code'&redirect_uri='$location $SERVER/oauth/token | jq '.')
|
35
|
|
36
|
token_type=$(jq '.token_type' <<< $json_response)
|
37
|
access_token=$(jq '.access_token' <<< $json_response | tr -d '\"')
|
38
|
|
39
|
if [ $token_type != '"bearer"' -o -z "$access_token" ] ; then
|
40
|
echo "Error no bearer access token received"
|
41
|
exit 1
|
42
|
fi
|
43
|
echo ' < bearer access token: '$access_token
|
44
|
echo ' > '$SERVER/classification.json' with authentication and access_token'
|
45
|
|
46
|
success=$(curl -v $client_secrets -H 'Authorization: bearer '$access_token $SERVER/classification.json 2>&1 | grep "< HTTP/1.1 200 OK")
|
47
|
echo ' '$success
|
48
|
if [ -n "$success" ]; then
|
49
|
echo "OK"
|
50
|
else
|
51
|
echo "ERROR"
|
52
|
fi
|
53
|
# ================================================================================
|
54
|
echo "2) Implicit Grant"
|
55
|
|
56
|
request_url=$SERVER'/oauth/authorize?response_type=token&client_id=any-client&redirect_uri='$SERVER'/user/me.json'
|
57
|
echo " > GET with basic authentication: $request_url"
|
58
|
params=$(curl -v -u admin:00000 "$request_url" 2>&1 | sed -n 's/< Location.*#//p')
|
59
|
# remove trailing line break!
|
60
|
params=${params%$'\r'}
|
61
|
echo ' < OAuth2-Response: '$params
|
62
|
echo ' > '$request_url
|
63
|
request_url=$SERVER'/user/me.json\?'${params}
|
64
|
success=$(curl -v "$request_url" 2>&1 | grep "< HTTP/1.1 200 OK")
|
65
|
echo ' '$success
|
66
|
if [ -n "$success" ]; then
|
67
|
echo "OK"
|
68
|
else
|
69
|
echo "ERROR"
|
70
|
fi
|
71
|
|