Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login with GitHub #135

Merged
merged 5 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@
<artifactId>azure-storage-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.ZipFileSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

Expand Down Expand Up @@ -108,7 +112,10 @@ private void triggerLoginEvent(@NonNull String accountType) {
}

@RequestMapping(path = "/", produces = "text/html")
public String home(Map<String, Object> model) {
public String home(Map<String, Object> model, OAuth2AuthenticationToken token) {
if (token != null && !StringUtils.isEmpty(token.getName())) {
model.put("loggedInUser", token.getPrincipal().getAttributes().get("login"));
}

this.addBuildInformation(model);
this.renderHome(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();
http.authorizeRequests().anyRequest().permitAll().and().oauth2Login().loginPage("/");

http.formLogin().disable()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").deleteCookies("JSESSIONID").invalidateHttpSession(true);

http.httpBasic().disable();
}
}
8 changes: 8 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
spring:
mustache:
expose-request-attributes: true
security:
oauth2:
client:
registration:
github:
client-id: xxxxxx
client-secret: xxxxxxx
scope: user, public_repo

playground:
env:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/static/css/customize.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ body, button, input, p, select, textarea, label, h1, h2, h3, h4 {
top: 0;
right: 0;
border: 0;
z-index: 1000;
}

.start-video {
Expand Down
34 changes: 34 additions & 0 deletions src/main/resources/static/js/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function() {
var $signInButton = $("#login_link");
var $signOutButton = $("#logout_link");
var $userDropdown = $("#user_dropdown");
var $loggedUser = $("#logged_user");

$signOutButton.on("click", function() {
logout();
});

function logout() {
var csrfToken = $("input[name='_csrf']").val();
var csrfTokenHeader = $("input[name='_csrf_header']").val();

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function () {
if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
loggedOutSuccess();
}
}

xhttp.open("POST", '/logout');
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader(csrfTokenHeader, csrfToken);
xhttp.send(null);
}

function loggedOutSuccess() {
$signInButton.removeClass("hidden");
$userDropdown.addClass("hidden");
$loggedUser.text(undefined);
}
}())
16 changes: 16 additions & 0 deletions src/main/resources/templates/home.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
Spring Cloud Azure Playground
</h1>
</div>
<div class="navbar-end azure-navbar-link {{#loggedInUser}}hidden{{/loggedInUser}}" id="login_link">
<a href="/oauth2/authorization/github">Login</a>
</div>
<div class="navbar-end azure-navbar-link {{^loggedInUser}}hidden{{/loggedInUser}}" id="user_dropdown">
<div class="navbar-item has-dropdown is-hoverable">
<a class="truncate" id="logged_user">
{{#loggedInUser}}{{loggedInUser}}{{/loggedInUser}}
</a>
<div class="navbar-dropdown" id="logout_link">
<a class="navbar-item">
Logout
</a>
</div>
</div>
</div>
</div>
</div>
</nav>
Expand Down Expand Up @@ -84,6 +99,7 @@
<script src="{{#linkTo}}/js/mousetrap.min.js{{/linkTo}}"></script>
<script src="{{#linkTo}}/js/adal.min.js{{/linkTo}}"></script>
<script src="{{#linkTo}}/js/all.js{{/linkTo}}"></script>
<script src="{{#linkTo}}/js/auth.js{{/linkTo}}"></script>
<script src="{{#linkTo}}/js/start.js{{/linkTo}}"></script>
</body>
</html>