Skip to content

Commit a783a4c

Browse files
oci functions auth example repo added
1 parent 5a97607 commit a783a4c

File tree

10 files changed

+283
-0
lines changed

10 files changed

+283
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
-->
38+
39+
# API Gateway authorizer function context var example
40+
41+
## Build and deploy the functions
42+
43+
### Authorizer function fnauthjs
44+
<pre>
45+
const fdk=require('@fnproject/fdk');
46+
47+
fdk.handle(function(input){
48+
let json = "";
49+
50+
if(input.token) {
51+
json = {
52+
"active": true,
53+
"principal": "myprincipal",
54+
"scope": ["fnsimplejs"],
55+
"clientId": "clientIdFromHeader",
56+
"expiresAt": "2023-12-31T00:00:00+00:00",
57+
"context": {
58+
"username": input.token
59+
}
60+
}
61+
} else {
62+
json = {
63+
"active": false,
64+
"expiresAt": "2023-12-31T00:00:00+00:00",
65+
"wwwAuthenticate": "Bearer realm=\"www.com\""
66+
}
67+
}
68+
return json;
69+
})
70+
</pre>
71+
72+
The authorizer function will pass on the <code>username</code> in <code>auth context</code> as a custom variable. The value for it is set from REST call input as <code>token</code> on the <a href="fnauthjs/func.js#L52">line 52</a>.
73+
<br>
74+
Here's the call using API Gateway:
75+
<pre>
76+
curl -H "token: test-token" https://drp....56kvgu.apigateway.eu-amsterdam-1.oci.customer-oci.com/
77+
</pre>
78+
Hence the auth context var <code>username</code> gets the value <code>test-token</code>
79+
80+
### Backend / secondary function fnsimplejs
81+
<pre>
82+
const fdk=require('@fnproject/fdk');
83+
84+
fdk.handle(function(input, ctx){
85+
return ctx.headers['Fn-Http-H-Username'];
86+
})
87+
</pre>
88+
89+
The secondary / backend function will get the authorizer passed variable <code>username</code>
90+
as a transformed header variable <code>Fn-Http-H-Username</code> and will print it out as the
91+
function REST call result on the <a href="fnsimplejs/func.js#L42">line 42</a>.
92+
<br>
93+
Here's the call using API Gateway:
94+
<pre>
95+
curl -H "token: test-token" https://drp....56kvgu.apigateway.eu-amsterdam-1.oci.customer-oci.com/
96+
["test-token"]
97+
</pre>
98+
99+
## Create the API Gateway based on the functions and configure as follows
100+
101+
To achieve this as described above create and configure API Gateway deployment as follows:
102+
103+
### Authorizer function fnauthjs
104+
105+
<img src="files/authorizer-function.png" width="800" />
106+
<p>
107+
108+
Use these settings for the <b><i>Single argument authorizer function</i></b>:
109+
<p>
110+
Token location: <b>Header</b>
111+
<br>
112+
Token header name: <b>token</b>
113+
114+
### Backend / secondary function fnsimplejs
115+
116+
<img src="files/backend-function.png" width="800" />
117+
<p>
118+
119+
### <i>auth context</i> variable <i>username</i> transformation in <i>Route Request Policies</i>
120+
121+
<img src="files/header-transformations.png" width="800" />
122+
<p>
123+
124+
Use these settings for the <b><i>Header transformations</i></b>:
125+
<p>
126+
Behavior: <b>Overwrite</b>
127+
<br>
128+
Header name: <b>username</b>
129+
<br>
130+
Values: <b>${request.auth[username]}</b>
131+
132+
## Authorization error 401 Unauthorized
133+
134+
<p>
135+
If no <code>token</code> is given in the API Gateway REST call the secondary/backend function will not be called and API Gateway will return <b>401 Unauthorized</b> error instead e.g.
136+
<pre>
137+
curl https://drp....56kvgu.apigateway.eu-amsterdam-1.oci.customer-oci.com/
138+
{"code":401,"message":"Unauthorized"}
139+
</pre>
140+
141+
243 KB
Loading
132 KB
Loading
50.8 KB
Loading
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
*/
38+
39+
const fdk=require('@fnproject/fdk');
40+
41+
fdk.handle(function(input){
42+
let json = "";
43+
44+
if(input.token) {
45+
json = {
46+
"active": true,
47+
"principal": "myprincipal",
48+
"scope": ["fnsimplejs"],
49+
"clientId": "clientIdFromHeader",
50+
"expiresAt": "2023-12-31T00:00:00+00:00",
51+
"context": {
52+
"username": input.token
53+
}
54+
}
55+
} else {
56+
json = {
57+
"active": false,
58+
"expiresAt": "2023-12-31T00:00:00+00:00",
59+
"wwwAuthenticate": "Bearer realm=\"www.com\""
60+
}
61+
}
62+
return json;
63+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
schema_version: 20180708
2+
name: fnauthjs
3+
version: 1.0.0
4+
runtime: node
5+
build_image: fnproject/node:14-dev
6+
run_image: fnproject/node:14
7+
entrypoint: node func.js
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "hellofn",
3+
"version": "1.0.0",
4+
"description": "example function",
5+
"main": "func.js",
6+
"author": "",
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"@fnproject/fdk": ">=0.0.54"
10+
}
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
*/
38+
39+
const fdk=require('@fnproject/fdk');
40+
41+
fdk.handle(function(input, ctx){
42+
return ctx.headers['Fn-Http-H-Username'];
43+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
schema_version: 20180708
2+
name: fnsimplejs
3+
version: 1.0.0
4+
runtime: node
5+
build_image: fnproject/node:14-dev
6+
run_image: fnproject/node:14
7+
entrypoint: node func.js
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "hellofn",
3+
"version": "1.0.0",
4+
"description": "example function",
5+
"main": "func.js",
6+
"author": "",
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"@fnproject/fdk": ">=0.0.54"
10+
}
11+
}

0 commit comments

Comments
 (0)