Browse Source
* feat: jooq generate * feat: support github oauth * feat: support oauth2 login * feat: update content-type * feat: add custom authentication exception * feat: add oauth2 app api * fix: checkstylemaster
vran
3 years ago
committed by
GitHub
52 changed files with 2120 additions and 63 deletions
@ -0,0 +1,87 @@ |
|||
package com.databasir.api; |
|||
|
|||
import com.databasir.common.JsonData; |
|||
import com.databasir.core.domain.app.OpenAuthAppService; |
|||
import com.databasir.core.domain.app.data.*; |
|||
import com.databasir.core.domain.app.handler.OpenAuthHandler; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.data.domain.Page; |
|||
import org.springframework.data.domain.Pageable; |
|||
import org.springframework.data.web.PageableDefault; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import javax.validation.Valid; |
|||
import java.util.List; |
|||
|
|||
import static org.springframework.data.domain.Sort.Direction.DESC; |
|||
|
|||
@Controller |
|||
@RequiredArgsConstructor |
|||
public class OpenAuth2AppController { |
|||
|
|||
private final OpenAuthHandler openAuthHandler; |
|||
|
|||
private final OpenAuthAppService openAuthAppService; |
|||
|
|||
/** |
|||
* 无需授权 |
|||
*/ |
|||
@GetMapping("/oauth2/authorization/{registrationId}") |
|||
@ResponseBody |
|||
public JsonData<String> authorization(@PathVariable String registrationId) { |
|||
String authorization = openAuthHandler.authorization(registrationId); |
|||
return JsonData.ok(authorization); |
|||
} |
|||
|
|||
/** |
|||
* 无需授权 |
|||
*/ |
|||
@GetMapping("/oauth2/apps") |
|||
@ResponseBody |
|||
public JsonData<List<OAuthAppResponse>> listApps() { |
|||
return JsonData.ok(openAuthAppService.listAll()); |
|||
} |
|||
|
|||
@GetMapping(Routes.OAuth2App.LIST_PAGE) |
|||
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") |
|||
@ResponseBody |
|||
public JsonData<Page<OAuthAppPageResponse>> listPage(@PageableDefault(sort = "id", direction = DESC) |
|||
Pageable page, |
|||
OAuthAppPageCondition condition) { |
|||
return JsonData.ok(openAuthAppService.listPage(page, condition)); |
|||
} |
|||
|
|||
@GetMapping(Routes.OAuth2App.GET_ONE) |
|||
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") |
|||
@ResponseBody |
|||
public JsonData<OAuthAppDetailResponse> getOne(@PathVariable Integer id) { |
|||
return JsonData.ok(openAuthAppService.getOne(id)); |
|||
|
|||
} |
|||
|
|||
@PostMapping(Routes.OAuth2App.CREATE) |
|||
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") |
|||
@ResponseBody |
|||
public JsonData<Integer> create(@RequestBody @Valid OAuthAppCreateRequest request) { |
|||
Integer id = openAuthAppService.create(request); |
|||
return JsonData.ok(id); |
|||
} |
|||
|
|||
@PatchMapping(Routes.OAuth2App.UPDATE) |
|||
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") |
|||
@ResponseBody |
|||
public JsonData<Void> updateById(@RequestBody @Valid OAuthAppUpdateRequest request) { |
|||
openAuthAppService.updateById(request); |
|||
return JsonData.ok(); |
|||
} |
|||
|
|||
@DeleteMapping(Routes.OAuth2App.DELETE) |
|||
@PreAuthorize("hasAnyAuthority('SYS_OWNER')") |
|||
@ResponseBody |
|||
public JsonData<Void> deleteById(@PathVariable Integer id) { |
|||
openAuthAppService.deleteById(id); |
|||
return JsonData.ok(); |
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
package com.databasir.api.config.oauth2; |
|||
|
|||
import org.springframework.security.authentication.AbstractAuthenticationToken; |
|||
import org.springframework.security.core.userdetails.UserDetails; |
|||
|
|||
public class DatabasirOAuth2Authentication extends AbstractAuthenticationToken { |
|||
|
|||
private Object credentials; |
|||
|
|||
private Object principal; |
|||
|
|||
public DatabasirOAuth2Authentication(UserDetails principal) { |
|||
super(principal.getAuthorities()); |
|||
this.credentials = null; |
|||
this.principal = principal; |
|||
setAuthenticated(false); |
|||
} |
|||
|
|||
@Override |
|||
public Object getCredentials() { |
|||
return this.credentials; |
|||
} |
|||
|
|||
@Override |
|||
public Object getPrincipal() { |
|||
return this.principal; |
|||
} |
|||
} |
@ -0,0 +1,62 @@ |
|||
package com.databasir.api.config.oauth2; |
|||
|
|||
import com.databasir.api.config.security.DatabasirUserDetailService; |
|||
import com.databasir.core.domain.user.data.UserDetailResponse; |
|||
import com.databasir.core.domain.app.OpenAuthAppService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.security.authentication.AuthenticationManager; |
|||
import org.springframework.security.authentication.DisabledException; |
|||
import org.springframework.security.core.Authentication; |
|||
import org.springframework.security.core.AuthenticationException; |
|||
import org.springframework.security.core.userdetails.UserDetails; |
|||
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
|||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.AntPathMatcher; |
|||
|
|||
import javax.servlet.ServletException; |
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.io.IOException; |
|||
import java.util.Map; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
public class DatabasirOauth2LoginFilter extends AbstractAuthenticationProcessingFilter { |
|||
|
|||
public static final String OAUTH_LOGIN_URI = "/oauth2/login/*"; |
|||
|
|||
@Autowired |
|||
private OpenAuthAppService openAuthAppService; |
|||
|
|||
@Autowired |
|||
private DatabasirUserDetailService databasirUserDetailService; |
|||
|
|||
public DatabasirOauth2LoginFilter(AuthenticationManager authenticationManager, |
|||
OAuth2AuthenticationSuccessHandler auth2AuthenticationSuccessHandler, |
|||
AuthenticationFailureHandler authenticationFailureHandler) { |
|||
super(OAUTH_LOGIN_URI, authenticationManager); |
|||
this.setAuthenticationSuccessHandler(auth2AuthenticationSuccessHandler); |
|||
this.setAuthenticationFailureHandler(authenticationFailureHandler); |
|||
} |
|||
|
|||
@Override |
|||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) |
|||
throws AuthenticationException, IOException, ServletException { |
|||
Map<String, String[]> params = request.getParameterMap(); |
|||
String registrationId = new AntPathMatcher().extractPathWithinPattern(OAUTH_LOGIN_URI, request.getRequestURI()); |
|||
UserDetailResponse userDetailResponse = openAuthAppService.oauthCallback(registrationId, params); |
|||
UserDetails details = databasirUserDetailService.loadUserByUsername(userDetailResponse.getUsername()); |
|||
DatabasirOAuth2Authentication authentication = new DatabasirOAuth2Authentication(details); |
|||
if (!userDetailResponse.getEnabled()) { |
|||
throw new DisabledException("账号已禁用"); |
|||
} |
|||
authentication.setAuthenticated(true); |
|||
if (log.isDebugEnabled()) { |
|||
log.debug("login {} success", registrationId); |
|||
} |
|||
return authentication; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,42 @@ |
|||
package com.databasir.api.config.oauth2; |
|||
|
|||
import com.databasir.api.config.security.DatabasirUserDetails; |
|||
import com.databasir.common.JsonData; |
|||
import com.databasir.core.domain.login.data.LoginKeyResponse; |
|||
import com.databasir.core.domain.login.data.UserLoginResponse; |
|||
import com.databasir.core.domain.login.service.LoginService; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.http.MediaType; |
|||
import org.springframework.security.authentication.CredentialsExpiredException; |
|||
import org.springframework.security.core.Authentication; |
|||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import javax.servlet.ServletException; |
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.io.IOException; |
|||
import java.nio.charset.StandardCharsets; |
|||
|
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class OAuth2AuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
|||
|
|||
private final LoginService loginService; |
|||
|
|||
private final ObjectMapper objectMapper; |
|||
|
|||
@Override |
|||
public void onAuthenticationSuccess(HttpServletRequest request, |
|||
HttpServletResponse response, |
|||
Authentication authentication) throws IOException, ServletException { |
|||
DatabasirUserDetails details = (DatabasirUserDetails) authentication.getPrincipal(); |
|||
LoginKeyResponse loginKey = loginService.generate(details.getUserPojo().getId()); |
|||
UserLoginResponse data = loginService.getUserLoginData(details.getUserPojo().getId()) |
|||
.orElseThrow(() -> new CredentialsExpiredException("请重新登陆")); |
|||
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
|||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); |
|||
objectMapper.writeValue(response.getWriter(), JsonData.ok(data)); |
|||
} |
|||
} |
@ -0,0 +1,111 @@ |
|||
package com.databasir.core.domain.app; |
|||
|
|||
import com.databasir.core.domain.DomainErrors; |
|||
import com.databasir.core.domain.app.converter.OAuthAppPojoConverter; |
|||
import com.databasir.core.domain.app.converter.OAuthAppResponseConverter; |
|||
import com.databasir.core.domain.app.data.*; |
|||
import com.databasir.core.domain.app.handler.OpenAuthHandler; |
|||
import com.databasir.core.domain.app.handler.OAuthProcessContext; |
|||
import com.databasir.core.domain.app.handler.OAuthProcessResult; |
|||
import com.databasir.core.domain.user.data.UserCreateRequest; |
|||
import com.databasir.core.domain.user.data.UserDetailResponse; |
|||
import com.databasir.core.domain.user.service.UserService; |
|||
import com.databasir.dao.impl.OauthAppDao; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.dao.DuplicateKeyException; |
|||
import org.springframework.data.domain.Page; |
|||
import org.springframework.data.domain.Pageable; |
|||
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class OpenAuthAppService { |
|||
|
|||
private final List<OpenAuthHandler> openAuthHandlers; |
|||
|
|||
private final OauthAppDao oauthAppDao; |
|||
|
|||
private final UserService userService; |
|||
|
|||
private final OAuthAppResponseConverter oauthAppResponseConverter; |
|||
|
|||
private final OAuthAppPojoConverter oauthAppPojoConverter; |
|||
|
|||
public UserDetailResponse oauthCallback(String registrationId, Map<String, String[]> params) { |
|||
|
|||
// match handler
|
|||
OauthAppPojo app = oauthAppDao.selectByRegistrationId(registrationId); |
|||
OpenAuthHandler openAuthHandler = openAuthHandlers.stream() |
|||
.filter(handler -> handler.support(app.getAppType())) |
|||
.findFirst() |
|||
.orElseThrow(() -> new UsernameNotFoundException("暂不支持该类型登陆")); |
|||
|
|||
// process by handler
|
|||
OAuthProcessContext context = OAuthProcessContext.builder() |
|||
.callbackParameters(params) |
|||
.registrationId(registrationId) |
|||
.build(); |
|||
OAuthProcessResult result = openAuthHandler.process(context); |
|||
|
|||
// get or create new user
|
|||
return userService.get(result.getEmail()) |
|||
.orElseGet(() -> { |
|||
UserCreateRequest user = new UserCreateRequest(); |
|||
user.setUsername(result.getUsername()); |
|||
user.setNickname(result.getNickname()); |
|||
user.setEmail(result.getEmail()); |
|||
user.setAvatar(result.getAvatar()); |
|||
user.setEnabled(true); |
|||
user.setPassword(UUID.randomUUID().toString().substring(0, 6)); |
|||
Integer id = userService.create(user); |
|||
return userService.get(id); |
|||
}); |
|||
} |
|||
|
|||
public List<OAuthAppResponse> listAll() { |
|||
List<OauthAppPojo> apps = oauthAppDao.selectAll(); |
|||
return apps.stream() |
|||
.map(oauthAppResponseConverter::to) |
|||
.collect(Collectors.toList()); |
|||
} |
|||
|
|||
public void deleteById(Integer id) { |
|||
if (oauthAppDao.existsById(id)) { |
|||
oauthAppDao.deleteById(id); |
|||
} |
|||
} |
|||
|
|||
public void updateById(OAuthAppUpdateRequest request) { |
|||
OauthAppPojo pojo = oauthAppPojoConverter.of(request); |
|||
try { |
|||
oauthAppDao.updateById(pojo); |
|||
} catch (DuplicateKeyException e) { |
|||
throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception(); |
|||
} |
|||
} |
|||
|
|||
public Integer create(OAuthAppCreateRequest request) { |
|||
OauthAppPojo pojo = oauthAppPojoConverter.of(request); |
|||
try { |
|||
return oauthAppDao.insertAndReturnId(pojo); |
|||
} catch (DuplicateKeyException e) { |
|||
throw DomainErrors.REGISTRATION_ID_DUPLICATE.exception(); |
|||
} |
|||
} |
|||
|
|||
public Page<OAuthAppPageResponse> listPage(Pageable page, OAuthAppPageCondition condition) { |
|||
return oauthAppDao.selectByPage(page, condition.toCondition()).map(oauthAppPojoConverter::toPageResponse); |
|||
} |
|||
|
|||
public Optional<OAuthAppDetailResponse> getOne(Integer id) { |
|||
return oauthAppDao.selectOptionalById(id).map(oauthAppPojoConverter::toDetailResponse); |
|||
} |
|||
} |
@ -0,0 +1,26 @@ |
|||
package com.databasir.core.domain.app.converter; |
|||
|
|||
import com.databasir.core.domain.app.data.OAuthAppCreateRequest; |
|||
import com.databasir.core.domain.app.data.OAuthAppDetailResponse; |
|||
import com.databasir.core.domain.app.data.OAuthAppPageResponse; |
|||
import com.databasir.core.domain.app.data.OAuthAppUpdateRequest; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
import org.mapstruct.Mapper; |
|||
import org.mapstruct.Mapping; |
|||
|
|||
@Mapper(componentModel = "spring") |
|||
public interface OAuthAppPojoConverter { |
|||
|
|||
@Mapping(target = "id", ignore = true) |
|||
@Mapping(target = "createAt", ignore = true) |
|||
@Mapping(target = "updateAt", ignore = true) |
|||
OauthAppPojo of(OAuthAppCreateRequest request); |
|||
|
|||
@Mapping(target = "createAt", ignore = true) |
|||
@Mapping(target = "updateAt", ignore = true) |
|||
OauthAppPojo of(OAuthAppUpdateRequest request); |
|||
|
|||
OAuthAppPageResponse toPageResponse(OauthAppPojo pojo); |
|||
|
|||
OAuthAppDetailResponse toDetailResponse(OauthAppPojo pojo); |
|||
} |
@ -0,0 +1,11 @@ |
|||
package com.databasir.core.domain.app.converter; |
|||
|
|||
import com.databasir.core.domain.app.data.OAuthAppResponse; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
import org.mapstruct.Mapper; |
|||
|
|||
@Mapper(componentModel = "spring") |
|||
public interface OAuthAppResponseConverter { |
|||
|
|||
OAuthAppResponse to(OauthAppPojo pojo); |
|||
} |
@ -0,0 +1,35 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import javax.validation.constraints.NotNull; |
|||
|
|||
@Data |
|||
public class OAuthAppCreateRequest { |
|||
|
|||
@NotNull |
|||
private String registrationId; |
|||
|
|||
@NotBlank |
|||
private String appName; |
|||
|
|||
@NotBlank |
|||
private String appType; |
|||
|
|||
private String appIcon; |
|||
|
|||
@NotBlank |
|||
private String authUrl; |
|||
|
|||
@NotBlank |
|||
private String resourceUrl; |
|||
|
|||
@NotBlank |
|||
private String clientId; |
|||
|
|||
@NotBlank |
|||
private String clientSecret; |
|||
|
|||
private String scope; |
|||
} |
@ -0,0 +1,31 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
@Data |
|||
public class OAuthAppDetailResponse { |
|||
|
|||
private Integer id; |
|||
|
|||
private String appName; |
|||
|
|||
private String appIcon; |
|||
|
|||
private String appType; |
|||
|
|||
private String registrationId; |
|||
|
|||
private String clientId; |
|||
|
|||
private String clientSecret; |
|||
|
|||
private String authUrl; |
|||
|
|||
private String resourceUrl; |
|||
|
|||
private LocalDateTime updateAt; |
|||
|
|||
private LocalDateTime createAt; |
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import com.databasir.dao.Tables; |
|||
import lombok.Data; |
|||
import org.jooq.Condition; |
|||
import org.jooq.impl.DSL; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@Data |
|||
public class OAuthAppPageCondition { |
|||
|
|||
private String appNameContains; |
|||
|
|||
private String appType; |
|||
|
|||
public Condition toCondition() { |
|||
List<Condition> conditions = new ArrayList<>(); |
|||
if (appNameContains != null && !appNameContains.trim().equals("")) { |
|||
conditions.add(Tables.OAUTH_APP.APP_NAME.contains(appNameContains)); |
|||
} |
|||
if (appType != null) { |
|||
conditions.add(Tables.OAUTH_APP.APP_TYPE.eq(appType)); |
|||
} |
|||
return conditions.stream() |
|||
.reduce(Condition::and) |
|||
.orElse(DSL.trueCondition()); |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
@Data |
|||
public class OAuthAppPageResponse { |
|||
|
|||
private Integer id; |
|||
|
|||
private String appName; |
|||
|
|||
private String appIcon; |
|||
|
|||
private String appType; |
|||
|
|||
private String registrationId; |
|||
|
|||
private String clientId; |
|||
|
|||
private String authUrl; |
|||
|
|||
private String resourceUrl; |
|||
|
|||
private LocalDateTime updateAt; |
|||
|
|||
private LocalDateTime createAt; |
|||
|
|||
} |
@ -0,0 +1,28 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
@Data |
|||
@Builder |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
public class OAuthAppResponse { |
|||
|
|||
private Integer id; |
|||
|
|||
private String appName; |
|||
|
|||
private String appIcon; |
|||
|
|||
private String appType; |
|||
|
|||
private String registrationId; |
|||
|
|||
private LocalDateTime createAt; |
|||
|
|||
} |
@ -0,0 +1,39 @@ |
|||
package com.databasir.core.domain.app.data; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import javax.validation.constraints.NotNull; |
|||
|
|||
@Data |
|||
public class OAuthAppUpdateRequest { |
|||
|
|||
@NotNull |
|||
private Integer id; |
|||
|
|||
@NotBlank |
|||
private String registrationId; |
|||
|
|||
@NotBlank |
|||
private String appName; |
|||
|
|||
@NotBlank |
|||
private String appType; |
|||
|
|||
private String appIcon; |
|||
|
|||
@NotBlank |
|||
private String authUrl; |
|||
|
|||
@NotBlank |
|||
private String resourceUrl; |
|||
|
|||
@NotBlank |
|||
private String clientId; |
|||
|
|||
@NotBlank |
|||
private String clientSecret; |
|||
|
|||
private String scope; |
|||
|
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.databasir.core.domain.app.exception; |
|||
|
|||
import com.databasir.common.DatabasirException; |
|||
import org.springframework.security.core.AuthenticationException; |
|||
|
|||
public class DatabasirAuthenticationException extends AuthenticationException { |
|||
|
|||
public DatabasirAuthenticationException(DatabasirException databasirException) { |
|||
super(databasirException.getErrMessage(), databasirException); |
|||
} |
|||
|
|||
public DatabasirAuthenticationException(String msg) { |
|||
super(msg); |
|||
} |
|||
|
|||
public DatabasirAuthenticationException(String msg, Throwable cause) { |
|||
super(msg, cause); |
|||
} |
|||
} |
@ -0,0 +1,83 @@ |
|||
package com.databasir.core.domain.app.handler; |
|||
|
|||
import com.databasir.core.domain.DomainErrors; |
|||
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; |
|||
import com.databasir.core.infrastructure.remote.github.GithubRemoteService; |
|||
import com.databasir.dao.enums.OAuthAppType; |
|||
import com.databasir.dao.impl.OauthAppDao; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.jooq.tools.StringUtils; |
|||
import org.springframework.security.authentication.CredentialsExpiredException; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.util.UriComponentsBuilder; |
|||
|
|||
import java.util.Map; |
|||
|
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class GithubOpenAuthHandler implements OpenAuthHandler { |
|||
|
|||
private final GithubRemoteService githubRemoteService; |
|||
|
|||
private final OauthAppDao oauthAppDao; |
|||
|
|||
@Override |
|||
public boolean support(String oauthAppType) { |
|||
return OAuthAppType.GITHUB.isSame(oauthAppType); |
|||
} |
|||
|
|||
@Override |
|||
public String authorization(String registrationId) { |
|||
OauthAppPojo app = oauthAppDao.selectByRegistrationId(registrationId); |
|||
String authUrl = app.getAuthUrl(); |
|||
String clientId = app.getClientId(); |
|||
String authorizeUrl = authUrl + "/login/oauth/authorize"; |
|||
String url = UriComponentsBuilder.fromUriString(authorizeUrl) |
|||
.queryParam("client_id", clientId) |
|||
.queryParam("scope", "read:user user:email") |
|||
.encode() |
|||
.build() |
|||
.toUriString(); |
|||
return url; |
|||
} |
|||
|
|||
@Override |
|||
public OAuthProcessResult process(OAuthProcessContext context) { |
|||
OauthAppPojo authApp = oauthAppDao.selectByRegistrationId(context.getRegistrationId()); |
|||
String clientId = authApp.getClientId(); |
|||
String clientSecret = authApp.getClientSecret(); |
|||
String baseUrl = authApp.getResourceUrl(); |
|||
|
|||
Map<String, String[]> params = context.getCallbackParameters(); |
|||
String code = params.get("code")[0]; |
|||
JsonNode tokenNode = githubRemoteService.getToken(baseUrl, clientId, clientSecret, code) |
|||
.get("access_token"); |
|||
if (tokenNode == null) { |
|||
throw new DatabasirAuthenticationException(DomainErrors.NETWORK_ERROR.exception()); |
|||
} |
|||
String accessToken = tokenNode.asText(); |
|||
if (StringUtils.isBlank(accessToken)) { |
|||
throw new CredentialsExpiredException("授权失效,请重新登陆"); |
|||
} |
|||
String email = null; |
|||
for (JsonNode node : githubRemoteService.getEmail(baseUrl, accessToken)) { |
|||
if (node.get("primary").asBoolean()) { |
|||
email = node.get("email").asText(); |
|||
} |
|||
} |
|||
if (StringUtils.isBlank(email)) { |
|||
throw new CredentialsExpiredException("授权失效,请重新登陆"); |
|||
} |
|||
JsonNode profile = githubRemoteService.getProfile(baseUrl, accessToken); |
|||
String nickname = profile.get("name").asText(); |
|||
String avatar = profile.get("avatar_url").asText(); |
|||
OAuthProcessResult result = new OAuthProcessResult(); |
|||
result.setEmail(email); |
|||
result.setNickname(nickname); |
|||
result.setUsername(email); |
|||
result.setAvatar(avatar); |
|||
return result; |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
package com.databasir.core.domain.app.handler; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Builder |
|||
public class OAuthProcessContext { |
|||
|
|||
private String registrationId; |
|||
|
|||
@Builder.Default |
|||
private Map<String, String[]> callbackParameters = new HashMap<>(); |
|||
|
|||
} |
@ -0,0 +1,16 @@ |
|||
package com.databasir.core.domain.app.handler; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class OAuthProcessResult { |
|||
|
|||
private String email; |
|||
|
|||
private String username; |
|||
|
|||
private String nickname; |
|||
|
|||
private String avatar; |
|||
|
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.databasir.core.domain.app.handler; |
|||
|
|||
public interface OpenAuthHandler { |
|||
|
|||
boolean support(String oauthAppType); |
|||
|
|||
String authorization(String registrationId); |
|||
|
|||
OAuthProcessResult process(OAuthProcessContext context); |
|||
} |
@ -1,16 +0,0 @@ |
|||
package com.databasir.core.domain.user.data; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
|
|||
@Data |
|||
public class UserLoginRequest { |
|||
|
|||
@NotBlank |
|||
private String username; |
|||
|
|||
@NotBlank |
|||
private String password; |
|||
|
|||
} |
@ -0,0 +1,34 @@ |
|||
package com.databasir.core.infrastructure.remote; |
|||
|
|||
import com.databasir.core.infrastructure.remote.github.GithubApiClient; |
|||
import com.databasir.core.infrastructure.remote.github.GithubOauthClient; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import retrofit2.Retrofit; |
|||
import retrofit2.converter.jackson.JacksonConverterFactory; |
|||
|
|||
@Configuration |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class ClientConfig { |
|||
|
|||
@Bean |
|||
public GithubApiClient githubApiClient() { |
|||
Retrofit retrofit = new Retrofit.Builder() |
|||
.baseUrl("https://api.github.com") |
|||
.addConverterFactory(JacksonConverterFactory.create()) |
|||
.build(); |
|||
return retrofit.create(GithubApiClient.class); |
|||
} |
|||
|
|||
@Bean |
|||
public GithubOauthClient githubOauthClient() { |
|||
Retrofit retrofit = new Retrofit.Builder() |
|||
.baseUrl("https://github.com") |
|||
.addConverterFactory(JacksonConverterFactory.create()) |
|||
.build(); |
|||
return retrofit.create(GithubOauthClient.class); |
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
package com.databasir.core.infrastructure.remote.github; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import retrofit2.Call; |
|||
import retrofit2.http.GET; |
|||
import retrofit2.http.Header; |
|||
import retrofit2.http.Headers; |
|||
import retrofit2.http.Url; |
|||
|
|||
public interface GithubApiClient { |
|||
|
|||
@GET |
|||
@Headers(value = { |
|||
"Accept: application/json" |
|||
}) |
|||
Call<JsonNode> getEmail(@Url String url, @Header("Authorization") String token); |
|||
|
|||
@GET |
|||
@Headers(value = { |
|||
"Accept: application/json" |
|||
}) |
|||
Call<JsonNode> getProfile(@Url String url, @Header("Authorization") String token); |
|||
} |
@ -0,0 +1,20 @@ |
|||
package com.databasir.core.infrastructure.remote.github; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import retrofit2.Call; |
|||
import retrofit2.http.Headers; |
|||
import retrofit2.http.POST; |
|||
import retrofit2.http.QueryMap; |
|||
import retrofit2.http.Url; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public interface GithubOauthClient { |
|||
|
|||
@Headers(value = { |
|||
"Accept: application/json" |
|||
}) |
|||
@POST |
|||
Call<JsonNode> getAccessToken(@Url String url, @QueryMap Map<String, String> request); |
|||
|
|||
} |
@ -0,0 +1,74 @@ |
|||
package com.databasir.core.infrastructure.remote.github; |
|||
|
|||
import com.databasir.common.SystemException; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import retrofit2.Call; |
|||
import retrofit2.Response; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class GithubRemoteService { |
|||
|
|||
private static final String TOKEN_PREFIX = "token "; |
|||
|
|||
private final GithubApiClient githubApiClient; |
|||
|
|||
private final GithubOauthClient githubOauthClient; |
|||
|
|||
public JsonNode getToken(String baseUrl, |
|||
String clientId, |
|||
String clientSecret, |
|||
String code) { |
|||
TokenRequest request = TokenRequest.builder() |
|||
.client_id(clientId) |
|||
.client_secret(clientSecret) |
|||
.code(code) |
|||
.build(); |
|||
String path = "/login/oauth/access_token"; |
|||
String url = baseUrl + path; |
|||
return execute(githubOauthClient.getAccessToken(url, request.toMap())); |
|||
} |
|||
|
|||
public JsonNode getEmail(String baseUrl, String token) { |
|||
String path; |
|||
if (baseUrl.contains("api.github.com")) { |
|||
path = "/user/emails"; |
|||
} else { |
|||
path = "/api/v3/user/emails"; |
|||
} |
|||
String url = baseUrl + path; |
|||
return execute(githubApiClient.getEmail(url, TOKEN_PREFIX + token)); |
|||
} |
|||
|
|||
public JsonNode getProfile(String baseUrl, String token) { |
|||
String path; |
|||
if (baseUrl.contains("api.github.com")) { |
|||
path = "/user"; |
|||
} else { |
|||
path = "/api/v3/user"; |
|||
} |
|||
String url = baseUrl + path; |
|||
return execute(githubApiClient.getProfile(url, TOKEN_PREFIX + token)); |
|||
} |
|||
|
|||
private <T> T execute(Call<T> call) { |
|||
try { |
|||
Response<T> response = call.execute(); |
|||
if (!response.isSuccessful()) { |
|||
log.error("request error: " + call.request() + ", response = " + response); |
|||
throw new SystemException("Call Remote Error"); |
|||
} else { |
|||
T body = response.body(); |
|||
return body; |
|||
} |
|||
} catch (IOException e) { |
|||
throw new SystemException("System Error", e); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,32 @@ |
|||
package com.databasir.core.infrastructure.remote.github; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
@SuppressWarnings("checkstyle:all") |
|||
@Builder |
|||
public class TokenRequest { |
|||
|
|||
private String client_id; |
|||
|
|||
private String client_secret; |
|||
|
|||
private String code; |
|||
|
|||
private String redirect_uri; |
|||
|
|||
public Map<String, String> toMap() { |
|||
HashMap<String, String> map = new HashMap<>(); |
|||
map.put("client_id", client_id); |
|||
map.put("client_secret", client_secret); |
|||
map.put("code", code); |
|||
if (redirect_uri != null) { |
|||
map.put("redirect_uri", redirect_uri); |
|||
} |
|||
return map; |
|||
} |
|||
} |
@ -0,0 +1,199 @@ |
|||
/* |
|||
* This file is generated by jOOQ. |
|||
*/ |
|||
package com.databasir.dao.tables; |
|||
|
|||
|
|||
import com.databasir.dao.Databasir; |
|||
import com.databasir.dao.Keys; |
|||
import com.databasir.dao.tables.records.OauthAppRecord; |
|||
|
|||
import java.time.LocalDateTime; |
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
|
|||
import org.jooq.Field; |
|||
import org.jooq.ForeignKey; |
|||
import org.jooq.Identity; |
|||
import org.jooq.Name; |
|||
import org.jooq.Record; |
|||
import org.jooq.Row12; |
|||
import org.jooq.Schema; |
|||
import org.jooq.Table; |
|||
import org.jooq.TableField; |
|||
import org.jooq.TableOptions; |
|||
import org.jooq.UniqueKey; |
|||
import org.jooq.impl.DSL; |
|||
import org.jooq.impl.SQLDataType; |
|||
import org.jooq.impl.TableImpl; |
|||
|
|||
|
|||
/** |
|||
* oauth app info |
|||
*/ |
|||
@SuppressWarnings({ "all", "unchecked", "rawtypes" }) |
|||
public class OauthApp extends TableImpl<OauthAppRecord> { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** |
|||
* The reference instance of <code>databasir.oauth_app</code> |
|||
*/ |
|||
public static final OauthApp OAUTH_APP = new OauthApp(); |
|||
|
|||
/** |
|||
* The class holding records for this type |
|||
*/ |
|||
@Override |
|||
public Class<OauthAppRecord> getRecordType() { |
|||
return OauthAppRecord.class; |
|||
} |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.id</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, Integer> ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false).identity(true), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.registration_id</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> REGISTRATION_ID = createField(DSL.name("registration_id"), SQLDataType.VARCHAR(100).nullable(false), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.app_name</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> APP_NAME = createField(DSL.name("app_name"), SQLDataType.VARCHAR(128).nullable(false), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.app_icon</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> APP_ICON = createField(DSL.name("app_icon"), SQLDataType.VARCHAR(256).nullable(false).defaultValue(DSL.inline("", SQLDataType.VARCHAR)), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.app_type</code>. github, gitlab |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> APP_TYPE = createField(DSL.name("app_type"), SQLDataType.VARCHAR(64).nullable(false), this, "github, gitlab"); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.client_id</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> CLIENT_ID = createField(DSL.name("client_id"), SQLDataType.VARCHAR(256), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.client_secret</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> CLIENT_SECRET = createField(DSL.name("client_secret"), SQLDataType.VARCHAR(256), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.auth_url</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> AUTH_URL = createField(DSL.name("auth_url"), SQLDataType.VARCHAR(256), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.resource_url</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> RESOURCE_URL = createField(DSL.name("resource_url"), SQLDataType.VARCHAR(256), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.scope</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, String> SCOPE = createField(DSL.name("scope"), SQLDataType.VARCHAR(256), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.update_at</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, LocalDateTime> UPDATE_AT = createField(DSL.name("update_at"), SQLDataType.LOCALDATETIME(0).nullable(false).defaultValue(DSL.field("CURRENT_TIMESTAMP", SQLDataType.LOCALDATETIME)), this, ""); |
|||
|
|||
/** |
|||
* The column <code>databasir.oauth_app.create_at</code>. |
|||
*/ |
|||
public final TableField<OauthAppRecord, LocalDateTime> CREATE_AT = createField(DSL.name("create_at"), SQLDataType.LOCALDATETIME(0).nullable(false).defaultValue(DSL.field("CURRENT_TIMESTAMP", SQLDataType.LOCALDATETIME)), this, ""); |
|||
|
|||
private OauthApp(Name alias, Table<OauthAppRecord> aliased) { |
|||
this(alias, aliased, null); |
|||
} |
|||
|
|||
private OauthApp(Name alias, Table<OauthAppRecord> aliased, Field<?>[] parameters) { |
|||
super(alias, null, aliased, parameters, DSL.comment("oauth app info"), TableOptions.table()); |
|||
} |
|||
|
|||
/** |
|||
* Create an aliased <code>databasir.oauth_app</code> table reference |
|||
*/ |
|||
public OauthApp(String alias) { |
|||
this(DSL.name(alias), OAUTH_APP); |
|||
} |
|||
|
|||
/** |
|||
* Create an aliased <code>databasir.oauth_app</code> table reference |
|||
*/ |
|||
public OauthApp(Name alias) { |
|||
this(alias, OAUTH_APP); |
|||
} |
|||
|
|||
/** |
|||
* Create a <code>databasir.oauth_app</code> table reference |
|||
*/ |
|||
public OauthApp() { |
|||
this(DSL.name("oauth_app"), null); |
|||
} |
|||
|
|||
public <O extends Record> OauthApp(Table<O> child, ForeignKey<O, OauthAppRecord> key) { |
|||
super(child, key, OAUTH_APP); |
|||
} |
|||
|
|||
@Override |
|||
public Schema getSchema() { |
|||
return aliased() ? null : Databasir.DATABASIR; |
|||
} |
|||
|
|||
@Override |
|||
public Identity<OauthAppRecord, Integer> getIdentity() { |
|||
return (Identity<OauthAppRecord, Integer>) super.getIdentity(); |
|||
} |
|||
|
|||
@Override |
|||
public UniqueKey<OauthAppRecord> getPrimaryKey() { |
|||
return Keys.KEY_OAUTH_APP_PRIMARY; |
|||
} |
|||
|
|||
@Override |
|||
public List<UniqueKey<OauthAppRecord>> getUniqueKeys() { |
|||
return Arrays.asList(Keys.KEY_OAUTH_APP_UK_REGISTRATION_ID); |
|||
} |
|||
|
|||
@Override |
|||
public OauthApp as(String alias) { |
|||
return new OauthApp(DSL.name(alias), this); |
|||
} |
|||
|
|||
@Override |
|||
public OauthApp as(Name alias) { |
|||
return new OauthApp(alias, this); |
|||
} |
|||
|
|||
/** |
|||
* Rename this table |
|||
*/ |
|||
@Override |
|||
public OauthApp rename(String name) { |
|||
return new OauthApp(DSL.name(name), null); |
|||
} |
|||
|
|||
/** |
|||
* Rename this table |
|||
*/ |
|||
@Override |
|||
public OauthApp rename(Name name) { |
|||
return new OauthApp(name, null); |
|||
} |
|||
|
|||
// -------------------------------------------------------------------------
|
|||
// Row12 type methods
|
|||
// -------------------------------------------------------------------------
|
|||
|
|||
@Override |
|||
public Row12<Integer, String, String, String, String, String, String, String, String, String, LocalDateTime, LocalDateTime> fieldsRow() { |
|||
return (Row12) super.fieldsRow(); |
|||
} |
|||
} |
@ -0,0 +1,265 @@ |
|||
/* |
|||
* This file is generated by jOOQ. |
|||
*/ |
|||
package com.databasir.dao.tables.pojos; |
|||
|
|||
|
|||
import java.io.Serializable; |
|||
import java.time.LocalDateTime; |
|||
|
|||
|
|||
/** |
|||
* oauth app info |
|||
*/ |
|||
@SuppressWarnings({ "all", "unchecked", "rawtypes" }) |
|||
public class OauthAppPojo implements Serializable { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private Integer id; |
|||
private String registrationId; |
|||
private String appName; |
|||
private String appIcon; |
|||
private String appType; |
|||
private String clientId; |
|||
private String clientSecret; |
|||
private String authUrl; |
|||
private String resourceUrl; |
|||
private String scope; |
|||
private LocalDateTime updateAt; |
|||
private LocalDateTime createAt; |
|||
|
|||
public OauthAppPojo() {} |
|||
|
|||
public OauthAppPojo(OauthAppPojo value) { |
|||
this.id = value.id; |
|||
this.registrationId = value.registrationId; |
|||
this.appName = value.appName; |
|||
this.appIcon = value.appIcon; |
|||
this.appType = value.appType; |
|||
this.clientId = value.clientId; |
|||
this.clientSecret = value.clientSecret; |
|||
this.authUrl = value.authUrl; |
|||
this.resourceUrl = value.resourceUrl; |
|||
this.scope = value.scope; |
|||
this.updateAt = value.updateAt; |
|||
this.createAt = value.createAt; |
|||
} |
|||
|
|||
public OauthAppPojo( |
|||
Integer id, |
|||
String registrationId, |
|||
String appName, |
|||
String appIcon, |
|||
String appType, |
|||
String clientId, |
|||
String clientSecret, |
|||
String authUrl, |
|||
String resourceUrl, |
|||
String scope, |
|||
LocalDateTime updateAt, |
|||
LocalDateTime createAt |
|||
) { |
|||
this.id = id; |
|||
this.registrationId = registrationId; |
|||
this.appName = appName; |
|||
this.appIcon = appIcon; |
|||
this.appType = appType; |
|||
this.clientId = clientId; |
|||
this.clientSecret = clientSecret; |
|||
this.authUrl = authUrl; |
|||
this.resourceUrl = resourceUrl; |
|||
this.scope = scope; |
|||
this.updateAt = updateAt; |
|||
this.createAt = createAt; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.id</code>. |
|||
*/ |
|||
public Integer getId() { |
|||
return this.id; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.id</code>. |
|||
*/ |
|||
public void setId(Integer id) { |
|||
this.id = id; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.registration_id</code>. |
|||
*/ |
|||
public String getRegistrationId() { |
|||
return this.registrationId; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.registration_id</code>. |
|||
*/ |
|||
public void setRegistrationId(String registrationId) { |
|||
this.registrationId = registrationId; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_name</code>. |
|||
*/ |
|||
public String getAppName() { |
|||
return this.appName; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_name</code>. |
|||
*/ |
|||
public void setAppName(String appName) { |
|||
this.appName = appName; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_icon</code>. |
|||
*/ |
|||
public String getAppIcon() { |
|||
return this.appIcon; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_icon</code>. |
|||
*/ |
|||
public void setAppIcon(String appIcon) { |
|||
this.appIcon = appIcon; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_type</code>. github, gitlab |
|||
*/ |
|||
public String getAppType() { |
|||
return this.appType; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_type</code>. github, gitlab |
|||
*/ |
|||
public void setAppType(String appType) { |
|||
this.appType = appType; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.client_id</code>. |
|||
*/ |
|||
public String getClientId() { |
|||
return this.clientId; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.client_id</code>. |
|||
*/ |
|||
public void setClientId(String clientId) { |
|||
this.clientId = clientId; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.client_secret</code>. |
|||
*/ |
|||
public String getClientSecret() { |
|||
return this.clientSecret; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.client_secret</code>. |
|||
*/ |
|||
public void setClientSecret(String clientSecret) { |
|||
this.clientSecret = clientSecret; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.auth_url</code>. |
|||
*/ |
|||
public String getAuthUrl() { |
|||
return this.authUrl; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.auth_url</code>. |
|||
*/ |
|||
public void setAuthUrl(String authUrl) { |
|||
this.authUrl = authUrl; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.resource_url</code>. |
|||
*/ |
|||
public String getResourceUrl() { |
|||
return this.resourceUrl; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.resource_url</code>. |
|||
*/ |
|||
public void setResourceUrl(String resourceUrl) { |
|||
this.resourceUrl = resourceUrl; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.scope</code>. |
|||
*/ |
|||
public String getScope() { |
|||
return this.scope; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.scope</code>. |
|||
*/ |
|||
public void setScope(String scope) { |
|||
this.scope = scope; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.update_at</code>. |
|||
*/ |
|||
public LocalDateTime getUpdateAt() { |
|||
return this.updateAt; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.update_at</code>. |
|||
*/ |
|||
public void setUpdateAt(LocalDateTime updateAt) { |
|||
this.updateAt = updateAt; |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.create_at</code>. |
|||
*/ |
|||
public LocalDateTime getCreateAt() { |
|||
return this.createAt; |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.create_at</code>. |
|||
*/ |
|||
public void setCreateAt(LocalDateTime createAt) { |
|||
this.createAt = createAt; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
StringBuilder sb = new StringBuilder("OauthAppPojo ("); |
|||
|
|||
sb.append(id); |
|||
sb.append(", ").append(registrationId); |
|||
sb.append(", ").append(appName); |
|||
sb.append(", ").append(appIcon); |
|||
sb.append(", ").append(appType); |
|||
sb.append(", ").append(clientId); |
|||
sb.append(", ").append(clientSecret); |
|||
sb.append(", ").append(authUrl); |
|||
sb.append(", ").append(resourceUrl); |
|||
sb.append(", ").append(scope); |
|||
sb.append(", ").append(updateAt); |
|||
sb.append(", ").append(createAt); |
|||
|
|||
sb.append(")"); |
|||
return sb.toString(); |
|||
} |
|||
} |
@ -0,0 +1,539 @@ |
|||
/* |
|||
* This file is generated by jOOQ. |
|||
*/ |
|||
package com.databasir.dao.tables.records; |
|||
|
|||
|
|||
import com.databasir.dao.tables.OauthApp; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
import org.jooq.Field; |
|||
import org.jooq.Record1; |
|||
import org.jooq.Record12; |
|||
import org.jooq.Row12; |
|||
import org.jooq.impl.UpdatableRecordImpl; |
|||
|
|||
|
|||
/** |
|||
* oauth app info |
|||
*/ |
|||
@SuppressWarnings({ "all", "unchecked", "rawtypes" }) |
|||
public class OauthAppRecord extends UpdatableRecordImpl<OauthAppRecord> implements Record12<Integer, String, String, String, String, String, String, String, String, String, LocalDateTime, LocalDateTime> { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.id</code>. |
|||
*/ |
|||
public void setId(Integer value) { |
|||
set(0, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.id</code>. |
|||
*/ |
|||
public Integer getId() { |
|||
return (Integer) get(0); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.registration_id</code>. |
|||
*/ |
|||
public void setRegistrationId(String value) { |
|||
set(1, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.registration_id</code>. |
|||
*/ |
|||
public String getRegistrationId() { |
|||
return (String) get(1); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_name</code>. |
|||
*/ |
|||
public void setAppName(String value) { |
|||
set(2, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_name</code>. |
|||
*/ |
|||
public String getAppName() { |
|||
return (String) get(2); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_icon</code>. |
|||
*/ |
|||
public void setAppIcon(String value) { |
|||
set(3, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_icon</code>. |
|||
*/ |
|||
public String getAppIcon() { |
|||
return (String) get(3); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.app_type</code>. github, gitlab |
|||
*/ |
|||
public void setAppType(String value) { |
|||
set(4, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.app_type</code>. github, gitlab |
|||
*/ |
|||
public String getAppType() { |
|||
return (String) get(4); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.client_id</code>. |
|||
*/ |
|||
public void setClientId(String value) { |
|||
set(5, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.client_id</code>. |
|||
*/ |
|||
public String getClientId() { |
|||
return (String) get(5); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.client_secret</code>. |
|||
*/ |
|||
public void setClientSecret(String value) { |
|||
set(6, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.client_secret</code>. |
|||
*/ |
|||
public String getClientSecret() { |
|||
return (String) get(6); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.auth_url</code>. |
|||
*/ |
|||
public void setAuthUrl(String value) { |
|||
set(7, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.auth_url</code>. |
|||
*/ |
|||
public String getAuthUrl() { |
|||
return (String) get(7); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.resource_url</code>. |
|||
*/ |
|||
public void setResourceUrl(String value) { |
|||
set(8, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.resource_url</code>. |
|||
*/ |
|||
public String getResourceUrl() { |
|||
return (String) get(8); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.scope</code>. |
|||
*/ |
|||
public void setScope(String value) { |
|||
set(9, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.scope</code>. |
|||
*/ |
|||
public String getScope() { |
|||
return (String) get(9); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.update_at</code>. |
|||
*/ |
|||
public void setUpdateAt(LocalDateTime value) { |
|||
set(10, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.update_at</code>. |
|||
*/ |
|||
public LocalDateTime getUpdateAt() { |
|||
return (LocalDateTime) get(10); |
|||
} |
|||
|
|||
/** |
|||
* Setter for <code>databasir.oauth_app.create_at</code>. |
|||
*/ |
|||
public void setCreateAt(LocalDateTime value) { |
|||
set(11, value); |
|||
} |
|||
|
|||
/** |
|||
* Getter for <code>databasir.oauth_app.create_at</code>. |
|||
*/ |
|||
public LocalDateTime getCreateAt() { |
|||
return (LocalDateTime) get(11); |
|||
} |
|||
|
|||
// -------------------------------------------------------------------------
|
|||
// Primary key information
|
|||
// -------------------------------------------------------------------------
|
|||
|
|||
@Override |
|||
public Record1<Integer> key() { |
|||
return (Record1) super.key(); |
|||
} |
|||
|
|||
// -------------------------------------------------------------------------
|
|||
// Record12 type implementation
|
|||
// -------------------------------------------------------------------------
|
|||
|
|||
@Override |
|||
public Row12<Integer, String, String, String, String, String, String, String, String, String, LocalDateTime, LocalDateTime> fieldsRow() { |
|||
return (Row12) super.fieldsRow(); |
|||
} |
|||
|
|||
@Override |
|||
public Row12<Integer, String, String, String, String, String, String, String, String, String, LocalDateTime, LocalDateTime> valuesRow() { |
|||
return (Row12) super.valuesRow(); |
|||
} |
|||
|
|||
@Override |
|||
public Field<Integer> field1() { |
|||
return OauthApp.OAUTH_APP.ID; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field2() { |
|||
return OauthApp.OAUTH_APP.REGISTRATION_ID; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field3() { |
|||
return OauthApp.OAUTH_APP.APP_NAME; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field4() { |
|||
return OauthApp.OAUTH_APP.APP_ICON; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field5() { |
|||
return OauthApp.OAUTH_APP.APP_TYPE; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field6() { |
|||
return OauthApp.OAUTH_APP.CLIENT_ID; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field7() { |
|||
return OauthApp.OAUTH_APP.CLIENT_SECRET; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field8() { |
|||
return OauthApp.OAUTH_APP.AUTH_URL; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field9() { |
|||
return OauthApp.OAUTH_APP.RESOURCE_URL; |
|||
} |
|||
|
|||
@Override |
|||
public Field<String> field10() { |
|||
return OauthApp.OAUTH_APP.SCOPE; |
|||
} |
|||
|
|||
@Override |
|||
public Field<LocalDateTime> field11() { |
|||
return OauthApp.OAUTH_APP.UPDATE_AT; |
|||
} |
|||
|
|||
@Override |
|||
public Field<LocalDateTime> field12() { |
|||
return OauthApp.OAUTH_APP.CREATE_AT; |
|||
} |
|||
|
|||
@Override |
|||
public Integer component1() { |
|||
return getId(); |
|||
} |
|||
|
|||
@Override |
|||
public String component2() { |
|||
return getRegistrationId(); |
|||
} |
|||
|
|||
@Override |
|||
public String component3() { |
|||
return getAppName(); |
|||
} |
|||
|
|||
@Override |
|||
public String component4() { |
|||
return getAppIcon(); |
|||
} |
|||
|
|||
@Override |
|||
public String component5() { |
|||
return getAppType(); |
|||
} |
|||
|
|||
@Override |
|||
public String component6() { |
|||
return getClientId(); |
|||
} |
|||
|
|||
@Override |
|||
public String component7() { |
|||
return getClientSecret(); |
|||
} |
|||
|
|||
@Override |
|||
public String component8() { |
|||
return getAuthUrl(); |
|||
} |
|||
|
|||
@Override |
|||
public String component9() { |
|||
return getResourceUrl(); |
|||
} |
|||
|
|||
@Override |
|||
public String component10() { |
|||
return getScope(); |
|||
} |
|||
|
|||
@Override |
|||
public LocalDateTime component11() { |
|||
return getUpdateAt(); |
|||
} |
|||
|
|||
@Override |
|||
public LocalDateTime component12() { |
|||
return getCreateAt(); |
|||
} |
|||
|
|||
@Override |
|||
public Integer value1() { |
|||
return getId(); |
|||
} |
|||
|
|||
@Override |
|||
public String value2() { |
|||
return getRegistrationId(); |
|||
} |
|||
|
|||
@Override |
|||
public String value3() { |
|||
return getAppName(); |
|||
} |
|||
|
|||
@Override |
|||
public String value4() { |
|||
return getAppIcon(); |
|||
} |
|||
|
|||
@Override |
|||
public String value5() { |
|||
return getAppType(); |
|||
} |
|||
|
|||
@Override |
|||
public String value6() { |
|||
return getClientId(); |
|||
} |
|||
|
|||
@Override |
|||
public String value7() { |
|||
return getClientSecret(); |
|||
} |
|||
|
|||
@Override |
|||
public String value8() { |
|||
return getAuthUrl(); |
|||
} |
|||
|
|||
@Override |
|||
public String value9() { |
|||
return getResourceUrl(); |
|||
} |
|||
|
|||
@Override |
|||
public String value10() { |
|||
return getScope(); |
|||
} |
|||
|
|||
@Override |
|||
public LocalDateTime value11() { |
|||
return getUpdateAt(); |
|||
} |
|||
|
|||
@Override |
|||
public LocalDateTime value12() { |
|||
return getCreateAt(); |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value1(Integer value) { |
|||
setId(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value2(String value) { |
|||
setRegistrationId(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value3(String value) { |
|||
setAppName(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value4(String value) { |
|||
setAppIcon(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value5(String value) { |
|||
setAppType(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value6(String value) { |
|||
setClientId(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value7(String value) { |
|||
setClientSecret(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value8(String value) { |
|||
setAuthUrl(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value9(String value) { |
|||
setResourceUrl(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value10(String value) { |
|||
setScope(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value11(LocalDateTime value) { |
|||
setUpdateAt(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord value12(LocalDateTime value) { |
|||
setCreateAt(value); |
|||
return this; |
|||
} |
|||
|
|||
@Override |
|||
public OauthAppRecord values(Integer value1, String value2, String value3, String value4, String value5, String value6, String value7, String value8, String value9, String value10, LocalDateTime value11, LocalDateTime value12) { |
|||
value1(value1); |
|||
value2(value2); |
|||
value3(value3); |
|||
value4(value4); |
|||
value5(value5); |
|||
value6(value6); |
|||
value7(value7); |
|||
value8(value8); |
|||
value9(value9); |
|||
value10(value10); |
|||
value11(value11); |
|||
value12(value12); |
|||
return this; |
|||
} |
|||
|
|||
// -------------------------------------------------------------------------
|
|||
// Constructors
|
|||
// -------------------------------------------------------------------------
|
|||
|
|||
/** |
|||
* Create a detached OauthAppRecord |
|||
*/ |
|||
public OauthAppRecord() { |
|||
super(OauthApp.OAUTH_APP); |
|||
} |
|||
|
|||
/** |
|||
* Create a detached, initialised OauthAppRecord |
|||
*/ |
|||
public OauthAppRecord(Integer id, String registrationId, String appName, String appIcon, String appType, String clientId, String clientSecret, String authUrl, String resourceUrl, String scope, LocalDateTime updateAt, LocalDateTime createAt) { |
|||
super(OauthApp.OAUTH_APP); |
|||
|
|||
setId(id); |
|||
setRegistrationId(registrationId); |
|||
setAppName(appName); |
|||
setAppIcon(appIcon); |
|||
setAppType(appType); |
|||
setClientId(clientId); |
|||
setClientSecret(clientSecret); |
|||
setAuthUrl(authUrl); |
|||
setResourceUrl(resourceUrl); |
|||
setScope(scope); |
|||
setUpdateAt(updateAt); |
|||
setCreateAt(createAt); |
|||
} |
|||
|
|||
/** |
|||
* Create a detached, initialised OauthAppRecord |
|||
*/ |
|||
public OauthAppRecord(OauthAppPojo value) { |
|||
super(OauthApp.OAUTH_APP); |
|||
|
|||
if (value != null) { |
|||
setId(value.getId()); |
|||
setRegistrationId(value.getRegistrationId()); |
|||
setAppName(value.getAppName()); |
|||
setAppIcon(value.getAppIcon()); |
|||
setAppType(value.getAppType()); |
|||
setClientId(value.getClientId()); |
|||
setClientSecret(value.getClientSecret()); |
|||
setAuthUrl(value.getAuthUrl()); |
|||
setResourceUrl(value.getResourceUrl()); |
|||
setScope(value.getScope()); |
|||
setUpdateAt(value.getUpdateAt()); |
|||
setCreateAt(value.getCreateAt()); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.databasir.dao.enums; |
|||
|
|||
public enum OAuthAppType { |
|||
|
|||
GITHUB, GITLAB; |
|||
|
|||
public boolean isSame(String type) { |
|||
return this.name().equalsIgnoreCase(type); |
|||
} |
|||
} |
@ -0,0 +1,37 @@ |
|||
package com.databasir.dao.impl; |
|||
|
|||
import com.databasir.dao.exception.DataNotExistsException; |
|||
import com.databasir.dao.tables.pojos.OauthAppPojo; |
|||
import lombok.Getter; |
|||
import org.jooq.DSLContext; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Repository; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
import static com.databasir.dao.Tables.OAUTH_APP; |
|||
|
|||
@Repository |
|||
public class OauthAppDao extends BaseDao<OauthAppPojo> { |
|||
|
|||
@Autowired |
|||
@Getter |
|||
private DSLContext dslContext; |
|||
|
|||
public OauthAppDao() { |
|||
super(OAUTH_APP, OauthAppPojo.class); |
|||
} |
|||
|
|||
public Optional<OauthAppPojo> selectOptionByRegistrationId(String registrationId) { |
|||
return this.getDslContext() |
|||
.select(OAUTH_APP.fields()).from(OAUTH_APP).where(OAUTH_APP.REGISTRATION_ID.eq(registrationId)) |
|||
.fetchOptionalInto(OauthAppPojo.class); |
|||
} |
|||
|
|||
public OauthAppPojo selectByRegistrationId(String registrationId) { |
|||
return this.getDslContext() |
|||
.select(OAUTH_APP.fields()).from(OAUTH_APP).where(OAUTH_APP.REGISTRATION_ID.eq(registrationId)) |
|||
.fetchOptionalInto(OauthAppPojo.class) |
|||
.orElseThrow(() -> new DataNotExistsException("can not found oauth app by " + registrationId)); |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
CREATE TABLE IF NOT EXISTS oauth_app |
|||
( |
|||
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, |
|||
registration_id VARCHAR(100) NOT NULL, |
|||
app_name VARCHAR(128) NOT NULL, |
|||
app_icon VARCHAR(256) NOT NULL DEFAULT '', |
|||
app_type VARCHAR(64) NOT NULL COMMENT 'github, gitlab', |
|||
client_id VARCHAR(256), |
|||
client_secret VARCHAR(256), |
|||
auth_url VARCHAR(256), |
|||
resource_url VARCHAR(256), |
|||
scope VARCHAR(256), |
|||
update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
|||
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
|||
UNIQUE uk_registration_id (registration_id) |
|||
) CHARSET utf8mb4 |
|||
COLLATE utf8mb4_unicode_ci COMMENT 'oauth app info'; |
|||
|
Loading…
Reference in new issue