Wiwiz Auth API Integration Sample (Java/JSP)

<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
 
<%@ page import="java.net.URL"%>
<%@ page import="java.net.URLConnection"%>
<%@ page import="java.net.URLEncoder"%>
 
<%
String userkey = "246DD22C084BB40E";	// replace with your own user key
 
//****************************************************
// Gets incoming parameters
//****************************************************
 
String pTokencode = request.getParameter("tokencode");	// incoming parameter "tokencode"
String pSrvurl = request.getParameter("srvurl");		// incoming parameter "srvurl"
 
 
/* Put them into session object if necessary */
if(pTokencode != null)
	session.setAttribute("tokencode", pTokencode);
if(pSrvurl != null)	
	session.setAttribute("srvurl", pSrvurl);
%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<title> Wiwiz Auth API Usage Sample </title>
</head>
<body>
 
<form method="post">
 
User Name: <input type="text" name="username" />
<br>
 
Password: <input type="password" name="pswd" />
<br>
 
<input type="submit" name="login" value="Login" />
 
<%
if(request.getParameter("login") != null) {	// if "Login" button is clicked
 
	//****************************************************
	// Step 1. Do your business. E.g. check user login ... 
	//****************************************************
 
	boolean loginSuccess = false;
	//
	// Do something you need.
	// e.g. verify the user
	//      ......
	//
 
	loginSuccess = true; // assume login successful
 
	if(loginSuccess == false) {
 
		out.println("Login Failed!");	// if user login failed, show an error message
 
	} else {
 
		//****************************************************
		// Step 2. Do the pre-auth by calling Wiwiz Auth API
		// IMPORTANT: Do this on your server side(ASP, C#, JSP/Servlet, PHP...), 
		//            but DO NOT do this on your client side (HTML/Javascript)
		//****************************************************
 
		// parameter "action" : REQUIRED!
		// set it to "1" to authenticate the user
		// set it to "0" to block the user
		String action = "1";
 
		// parameter "tokencode": REQUIRED!
		// set identical to the incoming parameter
		String tokencode = (String) session.getAttribute("tokencode");
 
		// parameter "srvurl": REQUIRED!
		// set identical to the incoming parameter	
		String srvurl = (String) session.getAttribute("srvurl");
 
		// parameter "endtime" : OPTIONAL
		// Format: yyyy-mm-dd hh:MM:ss  e.g. 2012-05-31 21:39:00
		// set this parameter to set the time to close the user's Internet connection 
		// Note: the value must be url-encoded.  
		String endtime = URLEncoder.encode("2012-05-31 21:39:00", "utf-8");
 
		// parameter "postauth" : OPTIONAL
		// E.g. http://www.YourDomain.com
		// set this parameter to redirect to a specified URL after authenticated.
		// Note: the value should be url-encoded.  
		String postauth = "http://www.wiwiz.com";
 
		String parameters =
				"wiwiz_auth_api=1&ver=1.0"+ // parameter "wiwiz_auth_api" and "ver". Fixed value
				"&tokencode="+ tokencode +	// parameter "tokencode". See above
				"&userkey="+ userkey +		// parameter "userkey". Set your own User Key
				"&action="+ action +		// parameter "action". See above
				"&endtime="+ endtime +		// parameter "endtime". See above
				"&postauth="+ postauth; 	// parameter "postauth". See above
 
		URL u = new URL(srvurl);			// use the value of incoming parameter "srvurl" as the requesting url
		URLConnection uc = u.openConnection();
		uc.setDoOutput(true);
		uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		PrintWriter pw = new PrintWriter(uc.getOutputStream());
		pw.println(parameters);
		pw.close();
 
		BufferedReader in = new BufferedReader(
		new InputStreamReader(uc.getInputStream()));
		String verifycode = in.readLine();	// gets "verifycode" - the verification result from Wiwiz server side
		in.close();
 
		String userstring = "username:" + request.getParameter("username");	// sets the Track Data (Optional), e.g. username
 
		if(verifycode.startsWith("ERR")) {
			// if there is an error, show error code
			out.println("Error: "+ verifycode);
 
		} else {
			// OK, now. do Step 3.
 
			//****************************************************
			// Step 3. Complete the Authentication by calling Wiwiz Auth API
			//****************************************************	
			String redirectUrl = srvurl +		// use the value of incoming parameter "srvurl" as the redirection address
					"?wiwiz_auth_api_login=1"+	// parameter "wiwiz_auth_api_login"
					"&tokencode="+ tokencode +	// parameter "tokencode", set identical to the incoming parameter	
					"&verifycode="+ verifycode+	// parameter "verifycode", the verification result from Wiwiz server side
					"&userstring=" + userstring; // parameter "userstring" (Optional), the Track Data set by the user
 
			response.sendRedirect(redirectUrl);	// finally, do the redirection
		}
	}
}
%>
 
</form>
 
</body>
</html>

Comments are closed.