├── .gitignore
├── LICENSE
├── README.adoc
├── pom.xml
└── src
├── main
├── java
│ └── org
│ │ └── superbiz
│ │ ├── moviefun
│ │ ├── Comment.java
│ │ ├── LoadBalancerRegisterService.java
│ │ ├── Movie.java
│ │ ├── MoviesBean.java
│ │ ├── rest
│ │ │ ├── ApplicationConfig.java
│ │ │ ├── Language.java
│ │ │ ├── LoadDataResource.java
│ │ │ ├── MoviesMPJWTConfigurationProvider.java
│ │ │ ├── MoviesResource.java
│ │ │ └── Person.java
│ │ ├── sts
│ │ │ ├── MovieClaimsSourceResource.java
│ │ │ └── UserPreferences.java
│ │ └── utils
│ │ │ ├── Cipher.java
│ │ │ ├── CipherProduces.java
│ │ │ ├── DecryptedValue.java
│ │ │ └── TokenUtil.java
│ │ └── rest
│ │ └── GreetingResource.java
├── resources
│ ├── META-INF
│ │ └── persistence.xml
│ ├── account-test.json
│ ├── privateKey-pkcs1.pem
│ ├── privateKey-pkcs8.pem
│ ├── profile-oauth2.json
│ ├── publicKey.pem
│ └── route.json
├── tag
│ └── import
│ │ ├── account-alex.json
│ │ ├── account-john.json
│ │ ├── account-mark.json
│ │ ├── account-nick.json
│ │ ├── account-test.json
│ │ ├── api-claims-movies.json
│ │ ├── api-movies.json
│ │ ├── default-keysize-2048.json
│ │ ├── default-oauth2-profile.json
│ │ ├── movies-admin.json
│ │ ├── profile-oauth2-movies.json
│ │ └── route-movies.json
├── tomee-backup
│ └── resources.xml
├── tomee
│ └── tomee.xml
└── webapp
│ ├── WEB-INF
│ └── web.xml
│ ├── app
│ ├── app.less
│ ├── config.js
│ ├── img
│ │ ├── login.jpg
│ │ ├── logout-icon.png
│ │ ├── movie-icon.png
│ │ └── movie-logo.png
│ └── js
│ │ ├── app.js
│ │ ├── model
│ │ ├── auth.js
│ │ ├── login.js
│ │ ├── movie.js
│ │ └── movies.js
│ │ ├── start.js
│ │ ├── templates.js
│ │ ├── templates
│ │ ├── alert.handlebars
│ │ ├── container.handlebars
│ │ ├── load-data-link.handlebars
│ │ ├── login.handlebars
│ │ ├── main-table-paginator-button.handlebars
│ │ ├── main-table-row.handlebars
│ │ ├── main.handlebars
│ │ ├── movie-page.handlebars
│ │ └── movie.handlebars
│ │ ├── tools
│ │ ├── alert.view.js
│ │ ├── date.js
│ │ ├── gravatar.js
│ │ ├── header-wrapper.js
│ │ ├── http-signatures-js.umd.min.js
│ │ ├── i18n.js
│ │ ├── id.js
│ │ ├── jwk-js.umd.min.js
│ │ └── log.js
│ │ └── view
│ │ ├── container.js
│ │ ├── login.js
│ │ ├── main-table-paginator.js
│ │ ├── main-table-row.js
│ │ ├── main.js
│ │ ├── movie-page.view.js
│ │ └── movie.js
│ └── index.jsp
└── test
├── java
└── org
│ └── superbiz
│ ├── moviefun
│ ├── MoviesTest.java
│ ├── STSResource.java
│ ├── TokenErrorResponse.java
│ └── TokenResponse.java
│ └── rest
│ └── GreetingResourceTest.java
└── resources
├── META-INF
└── application-client.xml
├── Token1.json
├── Token2.json
└── arquillian.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
24 | .idea
25 | *.iml
26 | target/
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.adoc:
--------------------------------------------------------------------------------
1 | # Microprofile JWT Movie Chat
2 |
3 | This web application shows how to use Microprofile JWT to secure your microservices.
4 | It uses Apache TomEE as the default Microprofile implementation.
5 |
6 | # OAuth2 + JWT (STS)
7 |
8 | In order to work, this application requires an STS capable of generating signed JWT tokens.
9 | We use Tribestream API Gateway as it's a complete Security Token Service.
10 | It's also capable of doing routing and while enforcing permissions.
11 | If you want to know more, check out the website at https://tribestream.io
12 |
13 | To start the Tribestream API Gateway on a terminal execute the following command:
14 |
15 | mvn tag:run
16 |
17 | The following message indicates the Tribestream API Gateway is up and running:
18 |
19 | [INFO] Tribestream started http://localhost:8080/tag/
20 |
21 | You can login now using on a browser the following URL: http://localhost:8080/tag/ using `admin` for both the username and password.
22 |
23 | # Start the movie chat application
24 |
25 | On a new terminal execute the following command:
26 |
27 |
28 | mvn clean install -DskipTests tomee:run
29 |
30 | The following message indicates the application is up and running:
31 |
32 | 08-Mar-2019 11:34:07.760 INFO [main] sun.reflect.NativeMethodAccessorImpl.invoke Starting ProtocolHandler ["http-nio-8181"]
33 | 08-Mar-2019 11:34:07.765 INFO [main] sun.reflect.NativeMethodAccessorImpl.invoke Starting ProtocolHandler ["ajp-nio-8010"]
34 | 08-Mar-2019 11:34:07.767 INFO [main] sun.reflect.NativeMethodAccessorImpl.invoke Server startup in 6786 ms
35 |
36 |
37 | You can login now using on a browser the following URL: http://localhost:8080/movies
38 |
39 | You can test any of the following accounts:
40 | - Account `alex` with password `password` with create, update and delete role.
41 | - Account `john` with password `password` with just comments add role.
42 |
43 | For deeper exaplanation of Microprofile JWT using this application access: https://tribestream.io/guide/en/api-gateway/microservice-security-microprofile-jwt/current/
44 |
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/Comment.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun;
18 |
19 | import javax.persistence.Entity;
20 | import javax.persistence.GeneratedValue;
21 | import javax.persistence.GenerationType;
22 | import javax.persistence.Id;
23 | import javax.persistence.Temporal;
24 | import javax.persistence.TemporalType;
25 | import javax.xml.bind.annotation.XmlRootElement;
26 | import java.util.Date;
27 |
28 | @Entity
29 | @XmlRootElement(name = "comment")
30 | public class Comment {
31 | @Id
32 | @GeneratedValue(strategy = GenerationType.AUTO)
33 | private long id;
34 |
35 | private String author;
36 | private String email;
37 | private String comment;
38 |
39 | @Temporal(value = TemporalType.TIMESTAMP)
40 | private Date timestamp;
41 |
42 | public String getAuthor() {
43 | return author;
44 | }
45 |
46 | public void setAuthor(final String author) {
47 | this.author = author;
48 | }
49 |
50 | public String getComment() {
51 | return comment;
52 | }
53 |
54 | public void setComment(final String comment) {
55 | this.comment = comment;
56 | }
57 |
58 | public long getId() {
59 | return id;
60 | }
61 |
62 | public void setId(final long id) {
63 | this.id = id;
64 | }
65 |
66 | public Date getTimestamp() {
67 | return timestamp;
68 | }
69 |
70 | public void setTimestamp(final Date timestamp) {
71 | this.timestamp = timestamp;
72 | }
73 |
74 |
75 | public String getEmail() {
76 | return email;
77 | }
78 |
79 | public void setEmail(final String email) {
80 | this.email = email;
81 | }
82 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/Movie.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun;
18 |
19 | import javax.persistence.CascadeType;
20 | import javax.persistence.Entity;
21 | import javax.persistence.FetchType;
22 | import javax.persistence.GeneratedValue;
23 | import javax.persistence.GenerationType;
24 | import javax.persistence.Id;
25 | import javax.persistence.OneToMany;
26 | import javax.xml.bind.annotation.XmlRootElement;
27 | import java.util.Set;
28 |
29 | @Entity
30 | @XmlRootElement(name = "movie")
31 | public class Movie {
32 | @Id
33 | @GeneratedValue(strategy = GenerationType.AUTO)
34 | private long id;
35 |
36 | private String director;
37 | private String title;
38 | private int year;
39 | private String genre;
40 | private int rating;
41 |
42 | @OneToMany(targetEntity = Comment.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
43 | private Set comments;
44 |
45 | public Movie() {
46 | }
47 |
48 | public Movie(String title, String director, String genre, int rating, int year) {
49 | this.director = director;
50 | this.title = title;
51 | this.year = year;
52 | this.genre = genre;
53 | this.rating = rating;
54 | }
55 |
56 | public Movie(String director, String title, int year) {
57 | this.director = director;
58 | this.title = title;
59 | this.year = year;
60 | }
61 |
62 | public long getId() {
63 | return id;
64 | }
65 |
66 | public void setId(long id) {
67 | this.id = id;
68 | }
69 |
70 | public String getDirector() {
71 | return director;
72 | }
73 |
74 | public void setDirector(String director) {
75 | this.director = director;
76 | }
77 |
78 | public String getTitle() {
79 | return title;
80 | }
81 |
82 | public void setTitle(String title) {
83 | this.title = title;
84 | }
85 |
86 | public int getYear() {
87 | return year;
88 | }
89 |
90 | public void setYear(int year) {
91 | this.year = year;
92 | }
93 |
94 | public String getGenre() {
95 | return genre;
96 | }
97 |
98 | public void setGenre(String genre) {
99 | this.genre = genre;
100 | }
101 |
102 | public int getRating() {
103 | return rating;
104 | }
105 |
106 | public void setRating(int rating) {
107 | this.rating = rating;
108 | }
109 |
110 | public Set getComments() {
111 | return comments;
112 | }
113 |
114 | public void setComments(final Set comments) {
115 | this.comments = comments;
116 | }
117 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/MoviesBean.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun;
18 |
19 | import javax.ejb.Lock;
20 | import javax.ejb.LockType;
21 | import javax.ejb.Singleton;
22 | import javax.persistence.EntityManager;
23 | import javax.persistence.PersistenceContext;
24 | import javax.persistence.TypedQuery;
25 | import javax.persistence.criteria.CriteriaBuilder;
26 | import javax.persistence.criteria.CriteriaQuery;
27 | import javax.persistence.criteria.Path;
28 | import javax.persistence.criteria.Predicate;
29 | import javax.persistence.criteria.Root;
30 | import javax.persistence.metamodel.EntityType;
31 | import java.util.List;
32 |
33 | @Singleton
34 | @Lock(LockType.READ)
35 | public class MoviesBean {
36 |
37 | @PersistenceContext(unitName = "movie-unit")
38 | private EntityManager entityManager;
39 |
40 | public Movie find(Long id) {
41 | return entityManager.find(Movie.class, id);
42 | }
43 |
44 | public Movie addMovie(Movie movie) {
45 | entityManager.persist(movie);
46 | return movie;
47 | }
48 |
49 | public void editMovie(Movie movie) {
50 | entityManager.merge(movie);
51 | }
52 |
53 | public void deleteMovie(long id) {
54 | Movie movie = entityManager.find(Movie.class, id);
55 | if (movie == null) {
56 | throw new IllegalArgumentException("Movie " + id + " not found");
57 | }
58 | entityManager.remove(movie);
59 | }
60 |
61 | public Movie addCommentToMovie(final Long id, final Comment comment) {
62 | final Movie movie = entityManager.find(Movie.class, id);
63 | if (movie == null) {
64 | throw new IllegalArgumentException("Movie " + id + " not found");
65 | }
66 | entityManager.persist(comment);
67 | movie.getComments().add(comment);
68 | entityManager.merge(movie);
69 | return movie;
70 | }
71 |
72 | public Movie removeCommentToMovie(final Long id, final Comment comment) {
73 | final Movie movie = entityManager.find(Movie.class, id);
74 | if (movie == null) {
75 | throw new IllegalArgumentException("Movie " + id + " not found");
76 | }
77 | movie.getComments().remove(comment);
78 | entityManager.remove(comment);
79 | entityManager.merge(movie);
80 | return movie;
81 | }
82 |
83 | public List getMovies(Integer firstResult, Integer maxResults, String field, String searchTerm) {
84 | CriteriaBuilder qb = entityManager.getCriteriaBuilder();
85 | CriteriaQuery cq = qb.createQuery(Movie.class);
86 | Root root = cq.from(Movie.class);
87 | EntityType type = entityManager.getMetamodel().entity(Movie.class);
88 | if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
89 | Path path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
90 | Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
91 | cq.where(condition);
92 | }
93 | TypedQuery q = entityManager.createQuery(cq);
94 | if (maxResults != null) {
95 | q.setMaxResults(maxResults);
96 | }
97 | if (firstResult != null) {
98 | q.setFirstResult(firstResult);
99 | }
100 | return q.getResultList();
101 | }
102 |
103 | public int count(String field, String searchTerm) {
104 | CriteriaBuilder qb = entityManager.getCriteriaBuilder();
105 | CriteriaQuery cq = qb.createQuery(Long.class);
106 | Root root = cq.from(Movie.class);
107 | EntityType type = entityManager.getMetamodel().entity(Movie.class);
108 | cq.select(qb.count(root));
109 | if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
110 | Path path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
111 | Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
112 | cq.where(condition);
113 | }
114 | return entityManager.createQuery(cq).getSingleResult().intValue();
115 | }
116 |
117 | public void clean() {
118 | entityManager.createQuery("delete from Movie").executeUpdate();
119 | }
120 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/rest/ApplicationConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.rest;
18 |
19 | import org.eclipse.microprofile.auth.LoginConfig;
20 |
21 | import javax.ws.rs.ApplicationPath;
22 | import javax.ws.rs.core.Application;
23 |
24 | @ApplicationPath("/rest")
25 | @LoginConfig(authMethod = "MP-JWT")
26 | public class ApplicationConfig extends Application {
27 | // let the server discover the endpoints
28 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/rest/Language.java:
--------------------------------------------------------------------------------
1 | package org.superbiz.moviefun.rest;
2 |
3 | public enum Language {
4 | SPANISH,
5 | ENGLISH;
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/rest/LoadDataResource.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.rest;
18 |
19 | import com.github.javafaker.Faker;
20 | import org.superbiz.moviefun.Comment;
21 | import org.superbiz.moviefun.Movie;
22 | import org.superbiz.moviefun.MoviesBean;
23 |
24 | import javax.ejb.EJB;
25 | import javax.ws.rs.POST;
26 | import javax.ws.rs.Path;
27 | import java.time.temporal.TemporalField;
28 | import java.util.Locale;
29 | import java.util.Random;
30 | import java.util.concurrent.TimeUnit;
31 |
32 | @Path("load")
33 | public class LoadDataResource {
34 |
35 | @EJB
36 | private MoviesBean moviesBean;
37 |
38 | @POST
39 | public void load() {
40 | final Faker faker = new Faker(Locale.ENGLISH);
41 | final Random random = new Random(System.nanoTime());
42 |
43 | for (int i = 0 ; i < (5 + random.nextInt(20)) ; i++) {
44 | addComments(
45 | moviesBean.addMovie(
46 | new Movie(
47 | faker.book().title(),
48 | faker.book().author(),
49 | faker.book().genre(),
50 | random.nextInt(10),
51 | 1960 + random.nextInt(50))),random.nextInt(100));
52 | }
53 |
54 | }
55 |
56 | private void addComments(final Movie movie, final int nbComments) {
57 |
58 | final Faker faker = new Faker(Locale.ENGLISH);
59 |
60 | for (int i = 0; i < nbComments; i++) {
61 | final Comment comment = new Comment();
62 | comment.setTimestamp(faker.date().past(300, TimeUnit.DAYS));
63 | comment.setAuthor(faker.name().fullName());
64 | comment.setEmail(faker.internet().emailAddress());
65 | comment.setComment(faker.chuckNorris().fact());
66 |
67 | moviesBean.addCommentToMovie(movie.getId(), comment);
68 | }
69 | }
70 |
71 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/rest/MoviesMPJWTConfigurationProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.rest;
18 |
19 | import java.security.KeyFactory;
20 | import java.security.interfaces.RSAPublicKey;
21 | import java.security.spec.X509EncodedKeySpec;
22 | import java.util.Optional;
23 |
24 | import javax.enterprise.context.Dependent;
25 | import javax.enterprise.inject.Produces;
26 | import org.apache.tomee.microprofile.jwt.config.JWTAuthConfiguration;
27 | import org.superbiz.moviefun.utils.TokenUtil;
28 |
29 | @Dependent
30 | public class MoviesMPJWTConfigurationProvider {
31 |
32 | public static final String ISSUED_BY = "/oauth2/token";
33 |
34 | @Produces
35 | Optional getOptionalContextInfo() throws Exception {
36 |
37 | byte[] encodedBytes = TokenUtil.readPublicKey("/publicKey.pem").getEncoded();
38 |
39 | final X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedBytes);
40 | final KeyFactory kf = KeyFactory.getInstance("RSA");
41 | final RSAPublicKey pk = (RSAPublicKey) kf.generatePublic(spec);
42 |
43 | JWTAuthConfiguration contextInfo = JWTAuthConfiguration.authContextInfo(pk, ISSUED_BY);
44 |
45 | return Optional.of(contextInfo);
46 | }
47 |
48 | @Produces
49 | JWTAuthConfiguration getContextInfo() throws Exception {
50 | return getOptionalContextInfo().get();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/rest/MoviesResource.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.sts;
18 |
19 | import javax.ws.rs.Consumes;
20 | import javax.ws.rs.POST;
21 | import javax.ws.rs.Path;
22 | import javax.ws.rs.Produces;
23 | import javax.ws.rs.core.MediaType;
24 | import java.util.HashMap;
25 | import java.util.Map;
26 |
27 | @Path("claims")
28 | public class MovieClaimsSourceResource {
29 |
30 | private static HashMap data = new HashMap<>();
31 |
32 | static {
33 | data.put("alex", new UserPreferences("SPANISH", "Guatemala JUG", "Mystery", "3211 1922 4433 1111"));
34 | data.put("john", new UserPreferences("ENGLISH", "Boston JUG", "Action", "2311 2345 8899 2222"));
35 | data.put("mark", new UserPreferences("ENGLISH", "London JUG", "Drama", "1122 6543 5858 3333"));
36 | data.put("nick", new UserPreferences("SPANISH", "Mexico JUG", "Comedy", "7789 8765 1222 4444"));
37 | }
38 |
39 | @POST
40 | @Produces(MediaType.APPLICATION_JSON)
41 | @Consumes(MediaType.APPLICATION_JSON)
42 | public UserPreferences authenticate(final Map payload) {
43 | String username = payload.get("username");
44 | return data.get(username);
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/sts/UserPreferences.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.sts;
18 |
19 | import org.superbiz.moviefun.utils.Cipher;
20 |
21 | import javax.xml.bind.annotation.XmlRootElement;
22 |
23 | @XmlRootElement(name = "preferences")
24 | public class UserPreferences {
25 |
26 | private String language;
27 | private String jug;
28 | private String creditCard;
29 | private String preferredGenre;
30 |
31 | public UserPreferences() {
32 | }
33 |
34 | public UserPreferences(final String language, final String jug, final String preferredGenre, final String creditCard) {
35 | this.language = language;
36 | this.jug = jug;
37 | this.creditCard = new String(Cipher.INSTANCE.getPasswordCipher().encrypt(creditCard));
38 | this.preferredGenre = preferredGenre;
39 | }
40 |
41 | public String getLanguage() {
42 | return language;
43 | }
44 |
45 | public String getJug() {
46 | return jug;
47 | }
48 |
49 | public String getCreditCard() {
50 | return creditCard;
51 | }
52 |
53 | public String getPreferredGenre() {
54 | return preferredGenre;
55 | }
56 |
57 | }
58 |
59 |
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/utils/Cipher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.utils;
18 |
19 | import org.apache.openejb.cipher.PasswordCipher;
20 | import org.apache.openejb.cipher.StaticDESPasswordCipher;
21 |
22 | public enum Cipher {
23 | INSTANCE;
24 |
25 | final PasswordCipher passwordCipher = new StaticDESPasswordCipher();
26 |
27 | public PasswordCipher getPasswordCipher() {
28 | return passwordCipher;
29 | }
30 | }
--------------------------------------------------------------------------------
/src/main/java/org/superbiz/moviefun/utils/CipherProduces.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.superbiz.moviefun.utils;
18 |
19 | import org.eclipse.microprofile.jwt.JsonWebToken;
20 |
21 | import javax.enterprise.context.ApplicationScoped;
22 | import javax.enterprise.context.RequestScoped;
23 | import javax.enterprise.inject.Produces;
24 | import javax.enterprise.inject.spi.InjectionPoint;
25 | import javax.inject.Inject;
26 | import java.util.Optional;
27 |
28 | @RequestScoped
29 | public class CipherProduces {
30 |
31 | @Inject
32 | private JsonWebToken jsonWebToken;
33 |
34 | @Produces
35 | @DecryptedValue
36 | public String decryptedCreditCard(InjectionPoint injectionPoint) {
37 | final DecryptedValue annotation = injectionPoint.getAnnotated().getAnnotation(DecryptedValue.class);
38 | final Optional