Skip to content

Commit

Permalink
Setup OAuth 2 authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
BilledTrain380 committed Sep 30, 2018
1 parent fcb317f commit 486610a
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 179 deletions.
22 changes: 22 additions & 0 deletions .idea/modules/sporttag-psa_main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/modules/sporttag-psa_test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ dependencies {
compile('org.flywaydb:flyway-core')
runtime('com.h2database:h2')

// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-oauth2
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-oauth2', version: '2.0.0.RELEASE'

// https://mvnrepository.com/artifact/org.passay/passay
compile group: 'org.passay', name: 'passay', version: '1.3.1'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2018 by Nicolas Märchy
*
* This file is part of Sporttag PSA.
*
* Sporttag PSA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sporttag PSA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sporttag PSA. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Sporttag PSA.
*
* Sporttag PSA ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Sporttag PSA wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*
*
*/

package ch.schulealtendorf.sporttagpsa.controller.config

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Configuration
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer
import org.springframework.security.oauth2.provider.token.TokenStore
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore

/**
* Configures OAuth 2 authorization server.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
@Configuration
@EnableAuthorizationServer
class AuthorizationServerConfig(
@Qualifier("authenticationManagerBean")
private val authenticationManager: AuthenticationManager
): AuthorizationServerConfigurerAdapter() {

override fun configure(security: AuthorizationServerSecurityConfigurer?) {

security
?.tokenKeyAccess("permitAll()")
?.checkTokenAccess("isAuthenticated()")
}

override fun configure(clients: ClientDetailsServiceConfigurer?) {

clients
?.inMemory()
?.withClient("psa-kitten")
?.autoApprove(true)
?.authorities("ADMIN", "USER")
?.authorizedGrantTypes("implicit")
?.accessTokenValiditySeconds(43200) // access token is valid for 12 hours
?.scopes("read", "write")
}

override fun configure(endpoints: AuthorizationServerEndpointsConfigurer?) {

endpoints
?.tokenStore(tokenStore())
?.authenticationManager(authenticationManager)
}

fun tokenStore(): TokenStore = InMemoryTokenStore()
}
Loading

0 comments on commit 486610a

Please sign in to comment.