Feature/oauth2 github (#27)

* 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: checkstyle
This commit is contained in:
vran
2022-03-02 20:13:25 +08:00
committed by GitHub
parent e799940c2d
commit cd20dfd7cf
52 changed files with 2120 additions and 63 deletions

View File

@@ -0,0 +1,10 @@
package com.databasir.dao.enums;
public enum OAuthAppType {
GITHUB, GITLAB;
public boolean isSame(String type) {
return this.name().equalsIgnoreCase(type);
}
}

View File

@@ -90,6 +90,12 @@ public abstract class BaseDao<R> {
.fetchInto(pojoType);
}
public List<R> selectAll() {
return this.getDslContext()
.selectFrom(table())
.fetchInto(pojoType);
}
public Page<R> selectByPage(Pageable request, Condition condition) {
Integer count = getDslContext()
.selectCount().from(table).where(condition)

View File

@@ -40,6 +40,13 @@ public class LoginDao extends BaseDao<LoginPojo> {
.fetchOptionalInto(LoginPojo.class);
}
public Optional<LoginPojo> selectByAccessToken(String accessToken) {
return getDslContext()
.selectFrom(LOGIN).where(LOGIN.ACCESS_TOKEN.eq(accessToken)
.and(LOGIN.ACCESS_TOKEN_EXPIRE_AT.ge(LocalDateTime.now())))
.fetchOptionalInto(LoginPojo.class);
}
public void updateAccessToken(String accessToken, LocalDateTime accessTokenExpireAt, Integer userId) {
getDslContext()
.update(LOGIN)

View File

@@ -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));
}
}

View File

@@ -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';