Skip to content

Commit

Permalink
✨ : add options in login page to choose oauth2 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Oct 16, 2019
1 parent 0cbcd07 commit 833f63b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 19 deletions.
14 changes: 0 additions & 14 deletions src/main/java/io/codeka/gaia/config/WebMvcConfig.java

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/java/io/codeka/gaia/login/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.codeka.gaia.login.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

private OAuth2ClientProperties oAuth2ClientProperties;

@Autowired(required = false)
public void setOAuth2ClientProperties(OAuth2ClientProperties oAuth2ClientProperties) {
this.oAuth2ClientProperties = oAuth2ClientProperties;
}

@GetMapping("/login")
public String login(Model model) {
if (oAuth2ClientProperties != null) {
model.addAttribute("clients", oAuth2ClientProperties.getRegistration().keySet());
}
return "login";
}
}
60 changes: 56 additions & 4 deletions src/main/resources/static/css/login.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h1,h2,h3,h4,h5,h6 {
h1, h2, h3, h4, h5, h6 {
margin: 0;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ button {
width: 100%;
height: 100%;
box-shadow: 0px 0px 0px 0px;
color: rgba(87,184,70, 0.8);
color: rgba(87, 184, 70, 0.8);
}

.symbol-input {
Expand Down Expand Up @@ -169,7 +169,6 @@ button {
}



/*------------------------------------------------------------------
[ Responsive ]*/

Expand Down Expand Up @@ -208,10 +207,63 @@ button {
}
}


/*------------------------------------------------------------------
[ Alert validate ]*/

.validate-input {
position: relative;
}

/*------------------------------------------------------------------
[ OAuth Signin options ]*/
.sign-in {
display: flex;
align-items: center;
color: #333333;
font-size: smaller;
margin: .5em 0;
text-align: center;
text-transform: uppercase;
}

.sign-in:before, .sign-in:after {
content: "";
border-top: .1em solid;
flex: 1;
margin: 0 .5em;
}

.login-oauth {
display: flex;
justify-content: space-evenly;
}

.login-oauth .btn {
font-size: 1.05em;
display: flex;
align-items: center;
}

.login-oauth .btn span {
margin-left: .5em;
}

.login-oauth .btn-gitlab {
color: #fca326;
}

.login-oauth .btn-gitlab:hover {
background-color: #fca326;
border-color: #fca326;
color: #fff;
}

.login-oauth .btn-github {
color: #333;
}

.login-oauth .btn-github:hover {
background-color: #333;
border-color: #333;
color: #fff;
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dashboard-widget text="Stacks to update" :value="toUpdateStackCount" variant="yellow_bg" icon="fa-caret-square-up" link="/stacks"></dashboard-widget>
</div>

<div sec:authorize="principal.username != 'admin'" v-if="! team" class="full_container">
<div sec:authorize="authentication.name != 'admin'" v-if="! team" class="full_container">
<div class="center">
<div class="error_page">
<div class="center">
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ <h1 class="login-form-title">Gaia - Login</h1>
</button>
</div>

<!-- OAuth2 connections -->
<div th:if="${clients != null and !clients.empty}">
<h7 class="sign-in">or Sign In with</h7>
<div class="login-oauth">
<a th:each="client: ${clients}"
th:href="@{/oauth2/authorization/{client}(client=${client})}"
th:class="${'btn btn-outline-secondary btn-sm btn-' + client}">
<i th:class="${'fab fa-' + client}"></i>
<span th:text="${client}"></span>
</a>
</div>
</div>

</form>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.codeka.gaia.login.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.ui.Model;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class LoginControllerTest {

private LoginController controller;

@Mock
OAuth2ClientProperties oAuth2ClientProperties;

@Mock
Model model;

@BeforeEach
void setup() {
controller = new LoginController();
controller.setOAuth2ClientProperties(oAuth2ClientProperties);
}

@Test
void login_shouldAddOAuth2ClientsInModel_whenConfigured() {
// given
var oAuth2clients = new HashMap<String, OAuth2ClientProperties.Registration>();
oAuth2clients.put("client_1", new OAuth2ClientProperties.Registration());
oAuth2clients.put("client_2", new OAuth2ClientProperties.Registration());
oAuth2clients.put("client_3", new OAuth2ClientProperties.Registration());

// when
when(oAuth2ClientProperties.getRegistration()).thenReturn(oAuth2clients);
controller.login(model);

// then
verify(model).addAttribute("clients", oAuth2clients.keySet());
}

@Test
void login_shouldIgnoreOAuth2_whenNotConfigured() {
// when
controller.setOAuth2ClientProperties(null);
controller.login(model);

// then
verify(model, never()).addAttribute(eq("clients"), any());
}

@Test
void login_shouldReturnTheCorrectView() {
// when
var result = controller.login(model);

// then
assertEquals("login", result);
}
}

0 comments on commit 833f63b

Please sign in to comment.