Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions 115_open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package auth

import (
"github.com/gin-gonic/gin"
"github.com/twoonefour/115-sdk-go"
"github.com/twoonefour/alist-auth/utils"
"net/http"
)

var (
clientID string
)

type TokenReq struct {
Uid string `json:"uid" binding:"required"`
CodeVerifier string `json:"code_verifier" binding:"required"`
}

func Open115Qrcode(c *gin.Context) {
client := sdk.New()
var cv string
var resp *sdk.AuthDeviceCodeResp
cv, err := utils.GenerateCodeVerifier(64)
if err != nil {
c.Error(err)
return
}
resp, err = client.AuthDeviceCode(c, clientID, cv)
if err != nil {
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"code_verifier": cv,
"resp": resp,
})
}

func Open115Token(c *gin.Context) {
client := sdk.New()
var req TokenReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var codeToTokenResp *sdk.CodeToTokenResp
codeToTokenResp, err := client.CodeToToken(c, req.Uid, req.CodeVerifier)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"resp": codeToTokenResp})
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 一个聚合driver oauth2的go后端

目前已实现
* onedrive https://alist-docs.voidval.com/zh/tool/onedrive/request.html
* dropbox https://alist-docs.voidval.com/zh/tool/dropbox/request.html
* wopan https://alist-docs.voidval.com/zh/tool/wopan/token.html (by xhofe)
12 changes: 6 additions & 6 deletions ali.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package alist
package auth

import (
"api.nn.ci/apps/common"
"api.nn.ci/utils"
"github.com/gin-gonic/gin"
"github.com/twoonefour/alist-auth/common"
"github.com/twoonefour/alist-auth/utils"
)

func Qr(c *gin.Context) {
Expand Down Expand Up @@ -64,9 +64,9 @@ func Ck(c *gin.Context) {
"referer": "https://passport.aliyundrive.com/mini_login.htm?&appName=aliyun_drive",
}).
Post("https://passport.aliyundrive.com/newlogin/qrcode/query.do?appName=aliyun_drive&fromSite=52&_bx-v=2.0.31")
//data := utils.Json.Get(res.Body(), "content", "data")
//loginResult := data.Get("loginResult").ToString()
//bizExt := data.Get("bizExt").ToString()
// data := utils.Json.Get(res.Body(), "content", "data")
// loginResult := data.Get("loginResult").ToString()
// bizExt := data.Get("bizExt").ToString()
c.Header("Content-Type", "application/json")
c.Writer.Write(res.Body())
}
6 changes: 3 additions & 3 deletions ali_open.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package alist
package auth

import (
"fmt"
"github.com/twoonefour/alist-auth/common"
"github.com/twoonefour/alist-auth/utils"
"strings"

"api.nn.ci/apps/common"
"api.nn.ci/utils"
"github.com/gin-gonic/gin"
)

Expand Down
8 changes: 4 additions & 4 deletions baidu.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package alist
package auth

import (
"fmt"
"github.com/twoonefour/alist-auth/common"
"github.com/twoonefour/alist-auth/utils"

"api.nn.ci/apps/common"
"api.nn.ci/utils"
"github.com/gin-gonic/gin"
)

var (
baiduClientId string
baiduClientSecret string
baiduCallbackUri = "https://alist.nn.ci/tool/baidu/callback"
baiduCallbackUri string
)

func baiduToken(c *gin.Context) {
Expand Down
15 changes: 15 additions & 0 deletions cmd/alist-oauth2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/gin-gonic/gin"
auth "github.com/twoonefour/alist-auth"
"github.com/twoonefour/alist-auth/utils"
)

func main() {
r := gin.New()
r.Use(utils.LoggerMiddleware())
api := r.Group("/alist")
auth.Setup(api)
r.Run(":3002")
}
41 changes: 41 additions & 0 deletions common/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

import (
"github.com/twoonefour/alist-auth/utils"
"net/http"

"github.com/gin-gonic/gin"
)

type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

func Error(c *gin.Context, err error) {
utils.GetLogger(c).Error("[ERROR] %v\n", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, Response{
Code: http.StatusInternalServerError,
Message: err.Error(),
})
}

func ErrorStr(c *gin.Context, message string) {
c.AbortWithStatusJSON(http.StatusInternalServerError, Response{
Code: http.StatusInternalServerError,
Message: message,
})
}

func ErrorJson(c *gin.Context, err interface{}, code ...int) {
c.AbortWithStatusJSON(http.StatusInternalServerError, Response{
Code: func() int {
if len(code) > 0 {
return code[0]
}
return http.StatusInternalServerError
}(),
Data: err,
})
}
17 changes: 17 additions & 0 deletions common/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common

import (
"github.com/gin-gonic/gin"
)

func JsonBytes(c *gin.Context, jsonBytes []byte) error {
c.Header("Content-Type", "application/json; charset=utf-8")
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
c.Writer.WriteHeaderNow()
if _, err := c.Writer.Write(jsonBytes); err != nil {
return err
}
return nil
}
112 changes: 112 additions & 0 deletions dropbox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package auth

import (
"github.com/gin-gonic/gin"
"github.com/twoonefour/alist-auth/common"
"github.com/twoonefour/alist-auth/utils"
)

var (
dropBoxAppSecret string
dropBoxAppId string
)

const (
DropboxAuthUrl = "https://api.dropboxapi.com/oauth2/token"
)

type AccessTokenReqData struct {
Code string `json:"code" form:"code"`
ClientId string `json:"client_id" form:"client_id"`
ClientSecret string `json:"client_secret" form:"client_secret"`
RefreshToken string `json:"refresh_token" form:"refresh_token"`
GrantType string `json:"grant_type" form:"grant_type"`
RedirectUri string `json:"redirect_uri" form:"redirect_uri"`
}

type AccessTokenResData struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
AccountId string `json:"account_id"`
Uid string `json:"uid"`
}

func getDropBoxToken(g *gin.Context) {
var accessTokenReqData AccessTokenReqData
err := g.Bind(&accessTokenReqData)
if err != nil {
common.Error(g, err)
return
}

if accessTokenReqData.GrantType == "refresh_token" {
refreshDropBoxToken(g, accessTokenReqData)
return
}

client := utils.RestyClient.R()
client.SetFormData(map[string]string{
"code": accessTokenReqData.Code,
"client_id": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppId
}
return accessTokenReqData.ClientId
}(),
"client_secret": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppSecret
}
return accessTokenReqData.ClientSecret
}(),
"grant_type": accessTokenReqData.GrantType,
"redirect_uri": accessTokenReqData.RedirectUri,
}).SetHeaders(map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
res, err := client.Execute("POST", DropboxAuthUrl)
if err != nil {
common.Error(g, err)
return
}
if err := common.JsonBytes(g, res.Body()); err != nil {
utils.GetLogger(g).Error(err)
return
}
}

func refreshDropBoxToken(g *gin.Context, accessTokenReqData AccessTokenReqData) {
if accessTokenReqData.GrantType != "refresh_token" {
common.ErrorStr(g, "Invalid grant type")
return
}
client := utils.RestyClient.R()
client.SetFormData(map[string]string{
"refresh_token": accessTokenReqData.RefreshToken,
"client_id": func() string {
if accessTokenReqData.ClientId == "" {
return dropBoxAppId
}
return accessTokenReqData.ClientId
}(),
"client_secret": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppSecret
}
return accessTokenReqData.ClientSecret
}(),
"grant_type": accessTokenReqData.GrantType,
}).SetHeaders(map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
res, err := client.Execute("POST", DropboxAuthUrl)
if err != nil {
common.Error(g, err)
return
}
common.JsonBytes(g, res.Body())
}
65 changes: 65 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module github.com/twoonefour/alist-auth

go 1.24.0

require (
github.com/axiaoxin-com/ratelimiter v1.0.3
github.com/gin-gonic/gin v1.10.1
github.com/go-resty/resty/v2 v2.16.5
github.com/sirupsen/logrus v1.4.2
github.com/twoonefour/115-sdk-go v0.1.5
github.com/xhofe/wopan-sdk-go v0.2.0
)

require (
github.com/axiaoxin-com/logging v1.2.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/getsentry/sentry-go v0.6.0 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/go-redis/redis/v8 v8.0.0-beta.10 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/jinzhu/gorm v1.9.12 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.10.0 // indirect
github.com/prometheus/procfs v0.1.3 // indirect
github.com/rs/xid v1.2.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xhofe/115-sdk-go v0.1.5 // indirect
go.opentelemetry.io/otel v0.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/arch v0.15.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20200821190819-94841d0725da // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
resty.dev/v3 v3.0.0-beta.1 // indirect
)
Loading