Skip to content

Commit

Permalink
fixed login form methods for LDAP and password auth
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Aug 14, 2017
1 parent 96459a3 commit ef4c69d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 64 deletions.
32 changes: 21 additions & 11 deletions src/main/java/com/erudika/scoold/controllers/SigninController.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,15 @@ public String get(@RequestParam(name = "returnto", required = false, defaultValu
}

@GetMapping(path = "/signin", params = {"access_token", "provider"})
public String getAuth(@RequestParam("access_token") String accessToken, @RequestParam("provider") String provider,
public String signinGet(@RequestParam("access_token") String accessToken, @RequestParam("provider") String provider,
HttpServletRequest req, HttpServletResponse res) {
if (!utils.isAuthenticated(req)) {
User u = utils.getParaClient().signIn(provider, accessToken, false);
if (u != null) {
HttpUtils.setStateParam(Config.AUTH_COOKIE, u.getPassword(), req, res, true);
} else {
verifyEmailIfNecessary(provider, "Anonymous", accessToken.split(":")[0], req);
return "redirect:" + signinlink + "?code=3&error=true";
}
}
return "redirect:" + getBackToUrl(req);
return getAuth(provider, accessToken, req, res);
}

@PostMapping(path = "/signin", params = {"access_token", "provider"})
public String signinPost(@RequestParam("access_token") String accessToken, @RequestParam("provider") String provider,
HttpServletRequest req, HttpServletResponse res) {
return getAuth(provider, accessToken, req, res);
}

@GetMapping("/signin/success")
Expand Down Expand Up @@ -171,6 +168,19 @@ public String globals(HttpServletRequest req, HttpServletResponse res) {
return sb.toString();
}

private String getAuth(String provider, String accessToken, HttpServletRequest req, HttpServletResponse res) {
if (!utils.isAuthenticated(req)) {
User u = utils.getParaClient().signIn(provider, accessToken, false);
if (u != null) {
HttpUtils.setStateParam(Config.AUTH_COOKIE, u.getPassword(), req, res, true);
} else {
verifyEmailIfNecessary(provider, "Anonymous", accessToken.split(":")[0], req);
return "redirect:" + signinlink + "?code=3&error=true";
}
}
return "redirect:" + getBackToUrl(req);
}

private String getBackToUrl(HttpServletRequest req) {
String backtoFromCookie = Utils.urlDecode(HttpUtils.getStateParam("returnto", req));
return (StringUtils.isBlank(backtoFromCookie) ? HOMEPAGE : backtoFromCookie);
Expand Down
58 changes: 15 additions & 43 deletions src/main/resources/static/scripts/signin.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
/* global FB_APP_ID, gapi, FB, GOOGLE_CLIENT_ID, GITHUB_APP_ID, LINKEDIN_APP_ID, APPID, ENDPOINT, TWITTER_APP_ID, MICROSOFT_APP_ID */

function onClick(buttonId, fn) {
var loginBtn = document.getElementById(buttonId);
if (loginBtn) {
if (document.body.attachEvent) {
loginBtn.attachEvent("onclick", fn);
} else {
loginBtn.addEventListener("click", fn);
}
}
}

function onSubmit(formId, fn) {
var form = document.getElementById(formId);
if (form) {
if (document.body.attachEvent) {
form.attachEvent("onsubmit", fn);
} else {
form.addEventListener("submit", fn);
}
}
}

/************************
* Facebook integration *
************************/
Expand All @@ -34,7 +11,7 @@ if (FB_APP_ID && FB_APP_ID.trim() !== "") {
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

onClick('fb-login-btn', function () {
$('#fb-login-btn').on('click', function () {
FB.login(function(response) {
if (response.authResponse) {
window.location = "/signin?provider=facebook&access_token=" + response.authResponse.accessToken;
Expand All @@ -58,16 +35,15 @@ if (GOOGLE_CLIENT_ID && GOOGLE_CLIENT_ID.trim() !== "") {
gjs.parentNode.insertBefore(js, gjs);
}(document, 'script', 'google-jssdk'));

var gpLoginBtn = document.getElementById('gp-login-btn');
function gpLogin() {
if (gpLoginBtn) {
if ($('gp-login-btn').length) {
gapi.load('auth2', function(){
auth2 = gapi.auth2.init({
client_id: GOOGLE_CLIENT_ID,
scope: 'https://www.googleapis.com/auth/plus.me'
});

auth2.attachClickHandler(document.getElementById('gp-login-btn'), {}, function(googleUser) {
auth2.attachClickHandler($('#gp-login-btn').get(0), {}, function(googleUser) {
window.location = "/signin?provider=google&access_token=" + googleUser.getAuthResponse(true).access_token;
}, function(error) {
window.location = "/signin?code=3&error=true";
Expand All @@ -80,7 +56,7 @@ if (GOOGLE_CLIENT_ID && GOOGLE_CLIENT_ID.trim() !== "") {
* GitHub integration *
**********************/
if (GITHUB_APP_ID && GITHUB_APP_ID.trim() !== "") {
onClick('gh-login-btn', function () {
$('#gh-login-btn').on('click', function () {
window.location = "https://github.com/login/oauth/authorize?" +
"response_type=code&client_id=" + GITHUB_APP_ID +
"&scope=user:email&state=" + (new Date().getTime()) +
Expand All @@ -92,7 +68,7 @@ if (GITHUB_APP_ID && GITHUB_APP_ID.trim() !== "") {
* LinkedIn integration *
************************/
if (LINKEDIN_APP_ID && LINKEDIN_APP_ID.trim() !== "") {
onClick('in-login-btn', function () {
$('#in-login-btn').on('click', function () {
window.location = "https://www.linkedin.com/uas/oauth2/authorization?" +
"response_type=code&client_id=" + LINKEDIN_APP_ID +
"&scope=r_emailaddress&state=" + (new Date().getTime()) +
Expand All @@ -104,7 +80,7 @@ if (LINKEDIN_APP_ID && LINKEDIN_APP_ID.trim() !== "") {
* Twitter integration *
***********************/
if (TWITTER_APP_ID && TWITTER_APP_ID.trim() !== "") {
onClick('tw-login-btn', function () {
$('#tw-login-btn').on('click', function () {
window.location = ENDPOINT + "/twitter_auth?appid=" + APPID;
return false;
});
Expand All @@ -113,7 +89,7 @@ if (TWITTER_APP_ID && TWITTER_APP_ID.trim() !== "") {
* Microsoft integration *
*************************/
if (MICROSOFT_APP_ID && MICROSOFT_APP_ID.trim() !== "") {
onClick('ms-login-btn', function () {
$('#ms-login-btn').on('click', function () {
window.location = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?" +
"response_type=code&client_id=" + MICROSOFT_APP_ID +
"&scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read&state=" + APPID +
Expand All @@ -124,24 +100,20 @@ if (MICROSOFT_APP_ID && MICROSOFT_APP_ID.trim() !== "") {
/********************
* LDAP integration *
********************/
onSubmit('ldap-login-form', function (e) {
e.preventDefault();
var username = document.getElementById("username");
var password = document.getElementById("password");
$('#ldap-login-form').on('submit', function () {
var username = $("#username").val();
var password = $("#password").val();
if (username && password) {
window.location = "/signin?provider=ldap&access_token=" + username.value + ":" + password.value;
$(this).find("input[name='access_token']").val(username + ":" + password);
}
return false;
});
/*******************
* Password Auth *
*******************/
onSubmit('password-login-form', function (e) {
e.preventDefault();
var email = document.getElementById("email");
var passw = document.getElementById("passw");
$('#password-login-form').on('submit', function () {
var email = $("#email").val();
var passw = $("#passw").val();
if (email && passw) {
window.location = "/signin?provider=password&access_token=" + email.value + "::" + passw.value;
$(this).find("input[name='access_token']").val(email + "::" + passw);
}
return false;
});
7 changes: 5 additions & 2 deletions src/main/resources/templates/base.vm
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.11.0/highlight.min.js"></script>
#end
<script src="$!scriptslink/scoold.js"></script>
#if($includeGAjs)
<script src="$!scriptslink/analytics.js"></script>
#if ($request.getRequestURI().startsWith($signinlink))
<script src="$!scriptslink/signin.js"></script>
#end
#if ($includeGAjs)
<script src="$!scriptslink/analytics.js"></script>
#end
</body>
</html>
20 changes: 12 additions & 8 deletions src/main/resources/templates/signin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@
#if($passwordLoginEnabled)
<h4>$!lang.get('signin.password')</h4>
<div class="card-panel white">
<form id="password-login-form">
<form method="post" id="password-login-form">
#sectoken(false "SIGNIN_PASS")
<div class="mvm">
<input id="email" type="email" name="email" value="" placeholder="Email" required>
<input id="email" type="email" value="" placeholder="Email" required>
</div>
<div class="mvm">
<input id="passw" type="password" name="passw" value="" minlength="6" placeholder="Password" required>
<input id="passw" type="password" value="" minlength="6" placeholder="Password" required>
</div>
<input type="hidden" name="provider" value="password">
<input type="hidden" name="access_token" value="">
<button type="submit" class="btn">$!lang.get('signin.title')</button>
</form>
<br>
Expand All @@ -77,13 +80,16 @@
#if($ldapLoginEnabled)
<h4>LDAP</h4>
<div class="card-panel white">
<form id="ldap-login-form">
<form method="post" id="ldap-login-form">
#sectoken(false "SIGNIN_LDAP")
<div class="mvm">
<input id="username" type="text" name="username" value="" placeholder="Username" required>
<input id="username" type="text" value="" placeholder="Username" required>
</div>
<div class="mvm">
<input id="password" type="password" name="password" value="" placeholder="Password" required>
<input id="password" type="password" value="" placeholder="Password" required>
</div>
<input type="hidden" name="provider" value="ldap">
<input type="hidden" name="access_token" value="">
<button type="submit" class="btn">$!lang.get('signin.title')</button>
</form>
</div>
Expand Down Expand Up @@ -146,5 +152,3 @@
</div>
</div>
#end

<script src="$scriptslink/signin.js"></script>

0 comments on commit ef4c69d

Please sign in to comment.