forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
81 lines (71 loc) · 1.91 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package osin
import (
"net/http"
"time"
)
// InfoRequest is a request for information about some AccessData
type InfoRequest struct {
Code string // Code to look up
AccessData *AccessData // AccessData associated with Code
}
// HandleInfoRequest is an http.HandlerFunc for server information
// NOT an RFC specification.
func (s *Server) HandleInfoRequest(w *Response, r *http.Request) *InfoRequest {
r.ParseForm()
bearer := CheckBearerAuth(r)
if bearer == nil {
w.SetError(E_INVALID_REQUEST, "")
return nil
}
// generate info request
ret := &InfoRequest{
Code: bearer.Code,
}
if ret.Code == "" {
w.SetError(E_INVALID_REQUEST, "")
return nil
}
var err error
// load access data
ret.AccessData, err = w.Storage.LoadAccess(ret.Code)
if err != nil {
w.SetError(E_INVALID_REQUEST, "")
w.InternalError = err
return nil
}
if ret.AccessData == nil {
w.SetError(E_INVALID_REQUEST, "")
return nil
}
if ret.AccessData.Client == nil {
w.SetError(E_UNAUTHORIZED_CLIENT, "")
return nil
}
if ret.AccessData.Client.GetRedirectUri() == "" {
w.SetError(E_UNAUTHORIZED_CLIENT, "")
return nil
}
if ret.AccessData.IsExpiredAt(s.Now()) {
w.SetError(E_INVALID_GRANT, "")
return nil
}
return ret
}
// FinishInfoRequest finalizes the request handled by HandleInfoRequest
func (s *Server) FinishInfoRequest(w *Response, r *http.Request, ir *InfoRequest) {
// don't process if is already an error
if w.IsError {
return
}
// output data
w.Output["client_id"] = ir.AccessData.Client.GetId()
w.Output["access_token"] = ir.AccessData.AccessToken
w.Output["token_type"] = s.Config.TokenType
w.Output["expires_in"] = ir.AccessData.CreatedAt.Add(time.Duration(ir.AccessData.ExpiresIn)*time.Second).Sub(s.Now()) / time.Second
if ir.AccessData.RefreshToken != "" {
w.Output["refresh_token"] = ir.AccessData.RefreshToken
}
if ir.AccessData.Scope != "" {
w.Output["scope"] = ir.AccessData.Scope
}
}