|
| 1 | +package com.hellokoding.springboot.oauth2; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 5 | +import com.github.scribejava.core.model.OAuthRequest; |
| 6 | +import com.github.scribejava.core.model.Verb; |
| 7 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PathVariable; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | +import org.springframework.web.servlet.view.RedirectView; |
| 14 | + |
| 15 | +import javax.servlet.http.HttpServletRequest; |
| 16 | +import javax.servlet.http.HttpServletResponse; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequiredArgsConstructor |
| 25 | +public class OAuth2Controller { |
| 26 | + private final OAuth2ServiceFactory oAuth2ServiceFactory; |
| 27 | + private final ObjectMapper objectMapper; |
| 28 | + |
| 29 | + @GetMapping("/oauth2/authorization/{serviceId}") |
| 30 | + public RedirectView oauth2Login(@PathVariable String serviceId, HttpServletRequest request) { |
| 31 | + String state = UUID.randomUUID().toString(); |
| 32 | + request.getSession().setAttribute("state", state); |
| 33 | + return new RedirectView(oAuth2ServiceFactory.getService(serviceId).getAuthorizationUrl(state)); |
| 34 | + } |
| 35 | + |
| 36 | + @GetMapping("/login/oauth2/code/{serviceId}") |
| 37 | + public RedirectView oauth2Code(@PathVariable String serviceId, String code, String state, HttpServletRequest request, HttpServletResponse response) throws InterruptedException, ExecutionException, IOException { |
| 38 | + if (!request.getSession().getAttribute("state").equals(state)) { |
| 39 | + response.setStatus(HttpStatus.UNAUTHORIZED.value()); |
| 40 | + } else { |
| 41 | + OAuth20Service oAuth20Service = oAuth2ServiceFactory.getService(serviceId); |
| 42 | + OAuth2AccessToken accessToken = oAuth20Service.getAccessToken(code); |
| 43 | + |
| 44 | + OAuth2ServiceFactory.OAuth2Api oAuth2Api = (OAuth2ServiceFactory.OAuth2Api)oAuth20Service.getApi(); |
| 45 | + final OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, oAuth2Api.getUserInfoEndpoint()); |
| 46 | + oAuth20Service.signRequest(accessToken, oAuthRequest); |
| 47 | + Map<String, String> map = objectMapper.readValue(oAuth20Service.execute(oAuthRequest).getBody(), Map.class); |
| 48 | + request.getSession().setAttribute("user", map); |
| 49 | + } |
| 50 | + |
| 51 | + return new RedirectView("/"); |
| 52 | + } |
| 53 | + |
| 54 | + @GetMapping("/user") |
| 55 | + public Map<String, Object> user(HttpServletRequest request) { |
| 56 | + Map<String, String> map = (Map)request.getSession().getAttribute("user"); |
| 57 | + return Collections.singletonMap("name", map.get("name")); |
| 58 | + } |
| 59 | +} |
0 commit comments