Skip to content
Merged
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
4 changes: 2 additions & 2 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {

srv := server.NewServer(server.NewConfig(), manager)
srv.SetUserAuthorizationHandler(userAuthorizeHandler)
srv.SetInternalErrorHandler(func(err error) {
fmt.Println("OAuth2 Error:", err.Error())
srv.SetInternalErrorHandler(func(r *http.Request, err error) {
fmt.Println("OAuth2 Error:", r.RequestURI, err.Error())
})

http.HandleFunc("/login", loginHandler)
Expand Down
2 changes: 1 addition & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RefreshingScopeHandler func(newScope, oldScope string) (allowed bool)
type ResponseErrorHandler func(re *errors.Response)

// InternalErrorHandler Internal error handing
type InternalErrorHandler func(err error)
type InternalErrorHandler func(req *http.Request, err error)

// ClientFormHandler Get client data from form
func ClientFormHandler(r *http.Request) (clientID, clientSecret string, err error) {
Expand Down
31 changes: 21 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ func (s *Server) GetAuthorizeData(rt oauth2.ResponseType, ti oauth2.TokenInfo) (
func (s *Server) GetErrorData(rerr, ierr error) (data map[string]interface{}, statusCode int) {
if ierr != nil {
rerr = errors.ErrServerError
if fn := s.InternalErrorHandler; fn != nil {
fn(ierr)
}
}
re := &errors.Response{
Error: rerr,
Expand All @@ -253,11 +250,18 @@ func (s *Server) GetErrorData(rerr, ierr error) (data map[string]interface{}, st
}

// response redirect error
func (s *Server) resRedirectError(w http.ResponseWriter, req *AuthorizeRequest, rerr, ierr error) (err error) {
func (s *Server) resRedirectError(w http.ResponseWriter, r *http.Request, req *AuthorizeRequest, rerr, ierr error) (err error) {
if req == nil {
err = ierr
return
}
if fn := s.InternalErrorHandler; fn != nil {
verr := ierr
if verr == nil {
verr = rerr
}
fn(r, verr)
}
data, _ := s.GetErrorData(rerr, ierr)
err = s.resRedirect(w, req, data)
return
Expand All @@ -283,20 +287,20 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
}()
req, rerr, ierr := s.ValidationAuthorizeRequest(r)
if rerr != nil || ierr != nil {
err = s.resRedirectError(w, req, rerr, ierr)
err = s.resRedirectError(w, r, req, rerr, ierr)
return
}
userID, err := s.UserAuthorizationHandler(w, r)
if err != nil {
err = s.resRedirectError(w, req, nil, err)
err = s.resRedirectError(w, r, req, nil, err)
return
} else if userID == "" {
return
}
req.UserID = userID
ti, rerr, ierr := s.GetAuthorizeToken(req)
if rerr != nil || ierr != nil {
err = s.resRedirectError(w, req, rerr, ierr)
err = s.resRedirectError(w, r, req, rerr, ierr)
return
}
err = s.resRedirect(w, req, s.GetAuthorizeData(req.ResponseType, ti))
Expand Down Expand Up @@ -460,19 +464,26 @@ func (s *Server) HandleTokenRequest(w http.ResponseWriter, r *http.Request) (err
}()
gt, tgr, rerr, ierr := s.ValidationTokenRequest(r)
if rerr != nil || ierr != nil {
err = s.resTokenError(w, rerr, ierr)
err = s.resTokenError(w, r, rerr, ierr)
return
}
ti, rerr, ierr := s.GetAccessToken(gt, tgr)
if rerr != nil || ierr != nil {
err = s.resTokenError(w, rerr, ierr)
err = s.resTokenError(w, r, rerr, ierr)
return
}
err = s.resToken(w, s.GetTokenData(ti))
return
}

func (s *Server) resTokenError(w http.ResponseWriter, rerr, ierr error) (err error) {
func (s *Server) resTokenError(w http.ResponseWriter, r *http.Request, rerr, ierr error) (err error) {
if fn := s.InternalErrorHandler; fn != nil {
verr := ierr
if verr == nil {
verr = rerr
}
fn(r, verr)
}
data, statusCode := s.GetErrorData(rerr, ierr)
s.resToken(w, data, statusCode)
return
Expand Down
78 changes: 48 additions & 30 deletions store/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,65 @@ type MemoryTokenStore struct {
gcInterval time.Duration
globalID int64
lock sync.RWMutex
basicList *list.List
data map[string]oauth2.TokenInfo
access map[string]string
refresh map[string]string
basicList *list.List
listLock sync.RWMutex
}

func (mts *MemoryTokenStore) gc() {
time.AfterFunc(mts.gcInterval, func() {
defer mts.gc()
mts.lock.RLock()
rmeles := make([]*list.Element, 0, 32)
mts.listLock.RLock()
ele := mts.basicList.Front()
mts.lock.RUnlock()
if ele == nil {
return
mts.listLock.RUnlock()
for ele != nil {
if rm := mts.gcElement(ele); rm {
rmeles = append(rmeles, ele)
}
mts.listLock.RLock()
ele = ele.Next()
mts.listLock.RUnlock()
}

for _, e := range rmeles {
mts.listLock.Lock()
mts.basicList.Remove(e)
mts.listLock.Unlock()
}
basicID := ele.Value.(string)
})
}

func (mts *MemoryTokenStore) gcElement(ele *list.Element) (rm bool) {
basicID := ele.Value.(string)
mts.lock.RLock()
ti, ok := mts.data[basicID]
mts.lock.RUnlock()
if !ok {
rm = true
return
}
ct := time.Now()
if refresh := ti.GetRefresh(); refresh != "" &&
ti.GetRefreshCreateAt().Add(ti.GetRefreshExpiresIn()).Before(ct) {
mts.lock.RLock()
ti, ok := mts.data[basicID]
delete(mts.access, ti.GetAccess())
delete(mts.refresh, refresh)
delete(mts.data, basicID)
mts.lock.RUnlock()
if !ok {
mts.lock.Lock()
mts.basicList.Remove(ele)
mts.lock.Unlock()
return
}
ct := time.Now()
if refresh := ti.GetRefresh(); refresh != "" &&
ti.GetRefreshCreateAt().Add(ti.GetRefreshExpiresIn()).Before(ct) {
mts.lock.RLock()
delete(mts.access, ti.GetAccess())
delete(mts.refresh, refresh)
rm = true
} else if ti.GetAccessCreateAt().Add(ti.GetAccessExpiresIn()).Before(ct) {
mts.lock.RLock()
delete(mts.access, ti.GetAccess())
if refresh := ti.GetRefresh(); refresh == "" {
delete(mts.data, basicID)
mts.basicList.Remove(ele)
mts.lock.RUnlock()
} else if ti.GetAccessCreateAt().Add(ti.GetAccessExpiresIn()).Before(ct) {
mts.lock.RLock()
delete(mts.access, ti.GetAccess())
if refresh := ti.GetRefresh(); refresh == "" {
delete(mts.data, basicID)
mts.basicList.Remove(ele)
}
mts.lock.RUnlock()
rm = true
}
})
mts.lock.RUnlock()
}
return
}

func (mts *MemoryTokenStore) getBasicID(id int64, info oauth2.TokenInfo) string {
Expand All @@ -92,7 +107,10 @@ func (mts *MemoryTokenStore) Create(info oauth2.TokenInfo) (err error) {
if refresh := info.GetRefresh(); refresh != "" {
mts.refresh[refresh] = basicID
}

mts.listLock.Lock()
mts.basicList.PushBack(basicID)
mts.listLock.Unlock()
return
}

Expand Down