Skip to content

Commit 33914db

Browse files
awb99awb99
and
awb99
authored
Mega refactoring (#13)
* local login working * oauth2 token flow working * cleanup * token-store connect working * github, google, xero working * rest requests working * get-auth-header working * rest examples * oidc login (almost) working * oidc workflow ok * oidc login working * readme * readme * cleanup error flow * readme --------- Co-authored-by: awb99 <[email protected]>
1 parent 2c91a73 commit 33914db

File tree

113 files changed

+2509
-1906
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+2509
-1906
lines changed

README.md

+28-33
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,48 @@
22

33

44
## core features
5-
- generate local-identity-token via user/password
6-
- run oauth2 workflow to get tokens from oauth2 providers (google, github, woo-commerce, xero, ...)
7-
- renew oauth2 tokens
8-
- tokens can be used to access rest/graphql apis
9-
- token store
105

11-
## gorilla dependencies
12-
- permission (define users and their permissions)
13-
- websocket (oauth via websocket and ring-handler)
14-
- modular (config, persistence)
6+
- oauth2 access-token engine
7+
- allow users to authorize the app via web interface from oauth2 providers (google, github, xero, ..)
8+
- get access-tokens for use with rest/graphql apis
9+
- transparently renews access-tokens
10+
- token store (rudimentary but necessary)
11+
- its primary use is to let your application use oauth2 protected services.
12+
- identity engine (to allow users to "login")
13+
- the idea is that you can expose clj services that are only accessible to certain users.
14+
- local-identity tokens via user/password
15+
- oidc tokens (using oauth2 flow)
1516

16-
# demo
1717

18-
The demo uses the extension manager from goldly to add oauth2 to goldly.
19-
20-
```
21-
cd demo
22-
clj -X:demo:npm-install
23-
clj -X:demo:compile
24-
clj -X:demo
25-
```
2618

27-
*local user/password login*
28-
Test local user/password login: user: "demo" password: "1234"
19+
# configuration
2920

30-
*oauth2 login*
31-
- oauth2 need a configuration before they work
3221
- [creds-empty.edn](https://github.com/pink-gorilla/oauth2/blob/main/creds-empty.edn) has empty values you can set and a description how to get the credentials
22+
- before starting the demo you need to set the correct credentials.
23+
- edit demo/deps.edn in :run alias set the :config to a file that contains your credentials.
3324

3425

35-
# demo - inspect received tokens
26+
# demo
27+
28+
```
29+
cd demo
30+
clj -X:webly:npm-install
31+
clj -X:webly:compile
32+
clj -X:webly:run
3633
```
37-
clj -X:run:token-info-google
34+
35+
Open a webbrowser on port 8080
3836

3937
```
4038
41-
# how to use tokens (for example in rest api):
39+
## gorilla dependencies
4240
43-
Have a look at [pink-gorilla/rest](https://github.com/pink-gorilla/rest) for rest-apis that use oauth2 tokens generated via this library.
41+
- permission (define users and their permissions)
42+
- clj-service (expose clj functions via websocket and ring-handler)
43+
- modular (edn persistence)
4444
45-
```
46-
(require '[modular.oauth2.token.refresh :as tr])
47-
(require '[modular.oauth2.token.info :refer [print-token-table]])
4845
49-
(tr/access-token-needs-refresh? :google)
50-
(tr/refresh-access-token :google)
46+
# how to use tokens (for example in rest api):
5147
52-
(print-token-table [:xero :shiphero :google :github])
53-
```
48+
Have a look at [pink-gorilla/rest](https://github.com/pink-gorilla/rest) for rest-apis that use oauth2 tokens generated via this library.
5449

TODO.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
# todo
12

3+
https://github.com/jarden-digital/jwt-verify-jwks
24

35
## github
46

57
- https://docs.github.com/en/rest/reference/users
68

7-
89
# links
910

1011
; AUTHENTICATION for web apps
@@ -46,11 +47,8 @@ https://github.com/ovotech/ring-jwt
4647
https://github.com/liquidz/clj-jwt/blob/master/src/clj_jwt/core.clj
4748

4849

49-
5050
[kitchen-async.promise :as p]
5151

52-
53-
https://github.com/liquidz/clj-jwt
5452
https://github.com/liquidz/clj-jwt
5553

5654
Certs from Google.
@@ -78,8 +76,6 @@ https://github.com/cjohansen/auth0-ring/tree/master/src/auth0_ring
7876

7977

8078

81-
82-
8379
of multiple endpoints for authenticating users, and for requesting resources including tokens, user information, and public keys.
8480

8581
To simplify implementations and increase flexibility, OpenID Connect allows the use of a "Discovery document," a JSON document found at a well-known location containing key-value pairs which provide details about the OpenID Connect provider's configuration, including the URIs of the authorization, token, revocation, userinfo, and public-keys endpoints. The Discovery document for Google's OpenID Connect service may be retrieved from:
@@ -219,3 +215,13 @@ const getAccessToken = async refreshToken => {
219215
console.log(err);
220216
}
221217
};
218+
219+
220+
221+
https://github.com/jarden-digital/jwt-verify-jwks
222+
223+
https://funcool.github.io/buddy-sign/latest/01-jwt.html
224+
225+
https://stackoverflow.com/questions/60497057/how-to-obtain-jwks-and-use-them-in-jwt-signing
226+
227+
https://github.com/liquidz/clj-jwt

build.clj

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
(ns build
22
(:require
3-
[babashka.fs :as fs]
4-
[clojure.java.io :as io]
5-
[clojure.string :as str]
63
[clojure.tools.build.api :as b]
74
[org.corfield.build :as bb] ; https://github.com/seancorfield/build-clj
8-
[deps-deploy.deps-deploy :as dd]))
5+
))
96

107

118
(def lib 'org.pinkgorilla/oauth2)
12-
(def version (format "0.0.%s" (b/git-count-revs nil)))
9+
(def version (format "0.2.%s" (b/git-count-revs nil)))
1310

1411
(defn jar "build the JAR" [opts]
1512
(println "Building the JAR")

creds-empty.edn

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

2-
{:oauth2 {:local {:client-secret ""}
3-
:google {:client-id ""
4-
:client-secret ""}
5-
:github {:client-id ""
6-
:client-secret ""}
7-
:xero {:client-id ""
8-
:client-secret ""}
9-
}}
2+
{:token {:local {:client-secret ""}
3+
:oauth2 {:google {:client-id ""
4+
:client-secret ""}
5+
:github {:client-id ""
6+
:client-secret ""}
7+
:xero {:client-id ""
8+
:client-secret ""}}
9+
10+
}}
1011

1112
; local
1213
; set "client-secret" to a random string which is used to encrypt tokens

demo/bb.edn

-20
This file was deleted.

demo/deps.edn

+6-31
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"target/webly"]
44

55
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
6-
org.pinkgorilla/webly {:mvn/version "0.5.655"}
7-
org.pinkgorilla/ui-tailwind {:mvn/version "0.0.5"}
6+
org.pinkgorilla/webly {:mvn/version "0.6.692"}
7+
org.pinkgorilla/ui-tailwind {:mvn/version "0.1.8"}
88
org.pinkgorilla/oauth2 {:local/root "../" :deps/manifest :deps}
9-
}
10-
9+
nrepl/nrepl {:mvn/version "1.2.0"}}
1110

1211
:aliases {:webly {:exec-fn webly.app.app/webly-build
1312
:exec-args {:config [{}]
@@ -22,35 +21,11 @@
2221

2322
:run {:exec-fn modular.system/start!
2423
:exec-args {:profile "jetty"
25-
:config ["oauth2/config.edn" ; oauth2 endpoints
26-
"demo-config.edn"
27-
"/home/florian/repo/myLinux/myvault/goldly/oauth2-localhost.edn" ; oauth2 secrets
24+
:config ["/home/florian/repo/myLinux/myvault/goldly/oauth2-localhost.edn" ; oauth2 secrets
2825
]
2926
:services "demo-services.edn"}}
30-
31-
;; token info
32-
:token-info-xero {:exec-args {:run demo.token/token-info
33-
:provider :xero}}
34-
35-
:token-info-google {:exec-args {:run demo.token/token-info
36-
:provider :google}}
37-
38-
:token-info-github {:exec-args {:run demo.token/token-info
39-
:provider :github}}
40-
41-
:tokens-summary {:exec-fn demo.token/tokens-summary
42-
:exec-args {:providers [:xero :github :google
43-
; :wonderland ; to test non existent tokens
44-
]}}
45-
46-
; token refresh
47-
:token-refresh-xero {:exec-fn demo.token-refresh/refresh-token-xero}
48-
49-
; clj
50-
51-
:password
52-
{:exec-fn demo.password/password
53-
:exec-args {:password "1234"}}}
27+
28+
}
5429

5530
;
5631
}

demo/resources/demo-config.edn

-16
This file was deleted.

demo/resources/demo-services.edn

+61-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,70 @@
66
:timbre {:start (modular.log/timbre-config!
77
(:timbre/clj (deref (clip/ref :config))))}
88

9+
:exts {:start (extension/discover)}
10+
11+
:permission {:start (modular.permission.core/start-permissions
12+
{:demo {:roles #{:logistic}
13+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
14+
:email ["[email protected]"]}
15+
:boss {:roles #{:logistic :supervisor :accounting}
16+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
17+
:email ["[email protected]"]}
18+
:florian {:roles #{:logistic}
19+
:password "a231498f6c1f441aa98482ea0b224ffa" ; 1234
20+
:email ["[email protected]"]}
21+
:john {:roles #{:logistic}
22+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
23+
:email ["[email protected]"]}})}
24+
25+
:clj-service {:start (clj-service.core/start-clj-services
26+
(clip/ref :permission)
27+
(clip/ref :exts))}
28+
29+
#_:identity/local #_{:start (token.identity.local/start-local-identity
30+
{:permission (clip/ref :permission)
31+
:clj (clip/ref :clj-service)
32+
:secret "AbHzj834ri9"})}
33+
34+
#_:identity/oidc #_{:start (token.identity.oidc/start-oidc-identity
35+
{:permission (clip/ref :permission)
36+
:clj (clip/ref :clj-service)})}
37+
38+
:identity {:start (token.identity.service/start-identity-service
39+
{:permission (clip/ref :permission)
40+
:clj (clip/ref :clj-service)
41+
:secret "AbHzj834ri9"})}
42+
43+
#_:oauth2/token-store #_{:start (token.oauth2.store/create-store {:clj (clip/ref :clj-service)
44+
:store-path ".webly/tokenstore"
45+
:store-role nil ; #{}
46+
})}
47+
48+
#_:oauth2 #_{:start (token.oauth2.core/start-oauth2-providers
49+
{:clj (clip/ref :clj-service)
50+
:store (clip/ref :oauth2/token-store)
51+
:providers (:oauth2 (:token (deref (clip/ref :config))))
52+
;:providers {:google {:client-id "" :client-secret ""}}
53+
})}
54+
55+
:oauth2 {:start (token.oauth2.service/start-oauth2-service
56+
{:clj (clip/ref :clj-service)
57+
:providers (:oauth2 (:token (deref (clip/ref :config))))
58+
;:providers {:google {:client-id "" :client-secret ""}}
59+
:store-path ".webly/tokenstore"
60+
:store-role nil ; #{}
61+
})}
62+
963

1064
:webly {:start (webly.app.app/start-webly
11-
(deref (clip/ref :config))
12-
(:profile #ref [:modular]))
13-
:stop (webly.app.app/stop-webly this)}
65+
(clip/ref :exts)
66+
(deref (clip/ref :config))
67+
(:profile #ref [:modular]))
68+
:stop (webly.app.app/stop-webly this)}
69+
1470

71+
:nrepl {:start (nrepl.server/start-server :bind "0.0.0.0" :port 9100)
72+
:stop (.close this)}
1573

1674

1775
;

demo/resources/ext/time-public.edn

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{:clj-services {:name "time-public"
2+
:permission nil
3+
:symbols [demo.time/time-public
4+
demo.time/time-debug]}}
5+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{:clj-services {:name "time-supervisor"
2+
:permission #{:supervisor}
3+
:symbols [demo.time/time-supervisor]}}
4+

demo/resources/ext/time-user.edn

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:clj-services {:name "time-user"
2+
:permission #{}
3+
:symbols [demo.time/time-user]}}

demo/resources/test-services.edn

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{:modular (modular.system/modular-env) ; {:profile "jetty" :config ["demo.edn" "secrets.edn"]}
2+
;:secrets #include "secrets.edn" ;; externalised and usually "mounted" as a docker/kubernetes secret
3+
:components
4+
{:config {:start (modular.config/load-config! (:config #ref [:modular]))}
5+
6+
:timbre {:start (modular.log/timbre-config!
7+
(:timbre/clj (deref (clip/ref :config))))}
8+
9+
:exts {:start (extension/discover)}
10+
11+
:permission {:start (modular.permission.core/start-permissions
12+
{:demo {:roles #{:logistic}
13+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
14+
:email ["[email protected]"]}
15+
:boss {:roles #{:logistic :supervisor :accounting}
16+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
17+
:email ["[email protected]"]}
18+
19+
:florian {:roles #{:logistic}
20+
:password "a231498f6c1f441aa98482ea0b224ffa" ; 1234
21+
:email ["[email protected]"]}
22+
:john {:roles #{:logistic}
23+
:password "a231498f6c1f441aa98482ea0b224ffa" ; "1234"
24+
:email ["[email protected]"]}})}
25+
:token {:start (token.oauth2.store/create-store {:path ".webly/tokenstore"})}
26+
27+
28+
:demo {:start (demo.token-info/run (clip/ref :token))}
29+
30+
;:clj-service {:start (clj-service.core/start-clj-services
31+
; (clip/ref :permission)
32+
; (clip/ref :exts))}
33+
34+
;:webly {:start (webly.app.app/start-webly
35+
; (clip/ref :exts)
36+
; (deref (clip/ref :config))
37+
; (:profile #ref [:modular]))
38+
; :stop (webly.app.app/stop-webly this)}
39+
40+
41+
42+
;
43+
}}

0 commit comments

Comments
 (0)