mirror of
https://github.com/vran-dev/databasir.git
synced 2025-09-21 11:19:21 +08:00
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:
10
dao/src/main/java/com/databasir/dao/enums/OAuthAppType.java
Normal file
10
dao/src/main/java/com/databasir/dao/enums/OAuthAppType.java
Normal 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);
|
||||
}
|
||||
}
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
37
dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java
Normal file
37
dao/src/main/java/com/databasir/dao/impl/OauthAppDao.java
Normal 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));
|
||||
}
|
||||
}
|
18
dao/src/main/resources/db/migration/V1.4__oauth.sql
Normal file
18
dao/src/main/resources/db/migration/V1.4__oauth.sql
Normal 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';
|
||||
|
Reference in New Issue
Block a user