Forked from joshlong/KotlinAuthenticationServiceApplication.kt
Created
December 10, 2017 00:54
-
-
Save zsiddiqi/64bfa5318feb4e1f706e4c433653336d to your computer and use it in GitHub Desktop.
An OAuth authorization service built using Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// org.springframework.cloud:spring-cloud-starter-oauth2 | |
// org.springframework.boot:spring-boot-starter-data-jpa | |
// com.h2database:h2 | |
// redefine: spring-security.version == 4.1.0.RELEASE | |
package com.example | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.CommandLineRunner | |
import org.springframework.boot.SpringApplication | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.context.annotation.Bean | |
import org.springframework.data.jpa.repository.JpaRepository | |
import org.springframework.security.authentication.AuthenticationManager | |
import org.springframework.security.core.authority.AuthorityUtils | |
import org.springframework.security.core.userdetails.User | |
import org.springframework.security.core.userdetails.UserDetailsService | |
import org.springframework.security.core.userdetails.UsernameNotFoundException | |
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.configuration.EnableResourceServer | |
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer | |
import org.springframework.web.bind.annotation.RequestMapping | |
import org.springframework.web.bind.annotation.RestController | |
import java.security.Principal | |
import java.util.* | |
import java.util.stream.Stream | |
import javax.persistence.Entity | |
import javax.persistence.GeneratedValue | |
import javax.persistence.Id | |
@EnableResourceServer | |
@EnableAuthorizationServer | |
@SpringBootApplication | |
@RestController | |
open class KotlinAuthenticationServiceApplication : | |
AuthorizationServerConfigurerAdapter() { | |
@Autowired | |
lateinit var authenticationManager: AuthenticationManager | |
@Bean | |
open fun userDetailsService(accountRepository: AccountRepository) = UserDetailsService { | |
username -> | |
accountRepository.findByUsername(username) | |
.map { account -> | |
User (account.username, | |
account.password, | |
account.active, | |
account.active, | |
account.active, | |
account.active, | |
AuthorityUtils.createAuthorityList("SCOPE_READ", | |
"SCOPE_ADMIN")) | |
} | |
.orElseThrow { | |
UsernameNotFoundException ( | |
"couldn't find the user ${username}!") | |
} | |
} | |
@Bean | |
open fun sampleDataCLR(accountRepository: AccountRepository): CommandLineRunner = | |
CommandLineRunner { | |
Stream.of("jlong,spring", "pwebb,boot", "dsyer,cloud") | |
.map { s -> s.split(",") } | |
.forEach { tuple -> | |
accountRepository.save( | |
Account(tuple[0], tuple[1], true)) | |
} | |
} | |
override fun configure(clients: ClientDetailsServiceConfigurer?) { | |
clients | |
?.inMemory() | |
?.withClient("acme") | |
?.secret("acmesecret") | |
?.authorizedGrantTypes("password") | |
?.scopes("openid") | |
} | |
override fun configure(endpoints: AuthorizationServerEndpointsConfigurer?) { | |
endpoints?.authenticationManager(this.authenticationManager) | |
} | |
@RequestMapping ("/user") | |
open fun user(principal: Principal) = principal | |
} | |
fun main(args: Array<String>) { | |
SpringApplication.run(KotlinAuthenticationServiceApplication::class.java, *args) | |
} | |
interface AccountRepository : JpaRepository <Account, Long> { | |
fun findByUsername(username: String): Optional <Account> | |
} | |
@Entity | |
open class Account(var username: String ? = null, | |
var password: String ? = null, | |
var active: Boolean = false) { | |
constructor() : this(null, null, false) | |
@Id | |
@GeneratedValue | |
var id: Long = 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment