Skip to content

Commit ad6ae39

Browse files
committed
Added new Auth demo
1 parent f29068c commit ad6ae39

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed

src/javaxt/demo/express/Auth.java

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package javaxt.demo.express;
2+
3+
import java.util.*;
4+
5+
import javaxt.http.servlet.*;
6+
7+
import javaxt.io.Directory;
8+
import static javaxt.utils.Console.*;
9+
10+
import javaxt.express.*;
11+
import static javaxt.demo.express.Utils.*;
12+
13+
14+
//******************************************************************************
15+
//** Auth
16+
//******************************************************************************
17+
/**
18+
* Used to run authentication-related demos
19+
*
20+
******************************************************************************/
21+
public class Auth {
22+
23+
private static String[] demos = new String[]{"BasicAuth","EmailLogin"};
24+
25+
26+
//**************************************************************************
27+
//** hasDemo
28+
//**************************************************************************
29+
public static boolean hasDemo(String demo){
30+
for (String d : demos){
31+
if (d.equalsIgnoreCase(demo)) return true;
32+
}
33+
return false;
34+
}
35+
36+
37+
//**************************************************************************
38+
//** start
39+
//**************************************************************************
40+
public static void start(Directory demoDir, HashMap<String, String> args){
41+
String demo = args.get("-demo");
42+
if (demo.equalsIgnoreCase("BasicAuth")){
43+
var webDir = new javaxt.io.Directory(demoDir + "auth/basic");
44+
basicAuth(webDir, args);
45+
}
46+
else if (demo.equalsIgnoreCase("EmailLogin")){
47+
emailAuth(demoDir, args);
48+
}
49+
else{
50+
System.out.println("Unknown/unsupported -demo value. Given \"" + demo + "\"");
51+
}
52+
}
53+
54+
55+
//**************************************************************************
56+
//** basicAuth
57+
//**************************************************************************
58+
/** Used to demonstrate basic authentication using a simple username and
59+
* password.
60+
*/
61+
private static void basicAuth(Directory web, HashMap<String, String> args){
62+
63+
//Prompt user to define a valid username and password
64+
String username = getInput("Username: ");
65+
String password = getPassword("Password: ");
66+
67+
68+
//Create an approved/authorized User
69+
var authorizedUser = new User(username, password);
70+
System.out.println(username + " user created!");
71+
72+
73+
//Create a simple Authenticator used to validate credentials associated
74+
//with an HTTP Request. In this demo we only have one approved User.
75+
//If the credentials match the username/password of the approved
76+
//user, then the user is returned via the getPrinciple(). If the
77+
//credentials are null or invalid, a null user is returned.
78+
var basicAuthenticator = new javaxt.express.Authenticator(){
79+
80+
public java.security.Principal getPrinciple(){
81+
82+
User user = null;
83+
try{
84+
String[] credentials = getCredentials();
85+
String username = credentials[0];
86+
String password = credentials[1];
87+
88+
if (username!=null && password!=null){
89+
90+
if (username.equalsIgnoreCase(authorizedUser.username) &&
91+
password.equals(authorizedUser.password)){
92+
user = authorizedUser;
93+
}
94+
}
95+
}
96+
catch(Exception e){}
97+
98+
setUser(user);
99+
return user;
100+
}
101+
};
102+
103+
104+
105+
//Create a custom HttpServlet
106+
var servlet = new HttpServlet() {
107+
108+
//Instantiate FileManager
109+
private FileManager fileManager = new FileManager(web);
110+
111+
//Assign authenticator to this servlet
112+
{this.setAuthenticator(basicAuthenticator);}
113+
114+
//
115+
public void processRequest(HttpServletRequest request, HttpServletResponse response)
116+
throws ServletException, java.io.IOException {
117+
118+
if (request.isWebSocket()) return;
119+
120+
121+
//Get path from url, excluding servlet path and leading "/" character
122+
String path = request.getPathInfo();
123+
if (path!=null) path = path.substring(1);
124+
125+
126+
//Get first "directory" in the path
127+
String service = path==null ? "" : path.toLowerCase();
128+
if (service.contains("/")) service = service.substring(0, service.indexOf("/"));
129+
130+
131+
132+
//Pass the request to the authenticator. Return early if the
133+
//authenticator handled the request.
134+
javaxt.express.Authenticator authenticator = (javaxt.express.Authenticator) getAuthenticator(request);
135+
if (authenticator.handleRequest(service, response)) return;
136+
137+
138+
139+
//Send static file if we can
140+
if (service.length()==0){
141+
142+
//If the service is empty, send welcome file (e.g. index.html)
143+
fileManager.sendFile(request, response);
144+
}
145+
else{
146+
147+
//Check if the service matches a file or folder in the web
148+
//directory. If so, send the static file as requested.
149+
for (Object obj : web.getChildren()){
150+
String name;
151+
if (obj instanceof javaxt.io.File){
152+
name = ((javaxt.io.File) obj).getName();
153+
}
154+
else{
155+
name = ((javaxt.io.Directory) obj).getName();
156+
}
157+
if (service.equalsIgnoreCase(name)){
158+
fileManager.sendFile(path, request, response);
159+
return;
160+
}
161+
}
162+
163+
}
164+
}
165+
166+
};
167+
168+
169+
//Start web server
170+
startServer(args, servlet);
171+
}
172+
173+
174+
//**************************************************************************
175+
//** emailAuth
176+
//**************************************************************************
177+
private static void emailAuth(Directory demoDir, HashMap<String, String> args){
178+
179+
180+
181+
//Create a simple Authenticator used to validate credentials associated
182+
//with an HTTP Request. In this demo we only have one approved User.
183+
//If the credentials match the username/password of the approved
184+
//user, then a
185+
var authenticator = new javaxt.express.Authenticator(){
186+
187+
public java.security.Principal getPrinciple(){
188+
189+
//Get user from cache
190+
User user = (User) getUser();
191+
if (user!=null) return user;
192+
193+
try{
194+
195+
String[] credentials = getCredentials();
196+
String email = credentials[0];
197+
String accessCode = credentials[1];
198+
199+
if (email!=null && accessCode!=null){
200+
201+
202+
}
203+
}
204+
catch(Exception e){
205+
}
206+
207+
setUser(user);
208+
return user;
209+
}
210+
211+
};
212+
213+
System.out.println("Not implemented...");
214+
}
215+
216+
217+
//**************************************************************************
218+
//** User Class
219+
//**************************************************************************
220+
/** Simple implementation of a User class
221+
*/
222+
private static class User
223+
implements java.security.Principal, javaxt.express.User {
224+
225+
private String username;
226+
private String password;
227+
public User(String username, String password){
228+
this.username = username;
229+
this.password = password;
230+
}
231+
232+
/** Returns a user ID - required for implementations of the
233+
* javaxt.express.User interface
234+
*/
235+
public Long getID(){
236+
return 1L;
237+
}
238+
239+
/** Returns a username/email - required for implementations of the
240+
* java.security.Principal interface.
241+
*/
242+
public String getName(){
243+
return username;
244+
}
245+
}
246+
247+
}

0 commit comments

Comments
 (0)