forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
71 lines (61 loc) · 1.68 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
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()
// generate info request
ret := &InfoRequest{
Code: r.Form.Get("code"),
}
if ret.Code == "" {
w.SetError(E_INVALID_REQUEST, "")
return nil
}
var err error
// load access data
ret.AccessData, err = s.Storage.LoadAccess(ret.Code)
if err != nil {
w.SetError(E_INVALID_REQUEST, "")
w.InternalError = err
return nil
}
if ret.AccessData.Client == nil {
w.SetError(E_UNAUTHORIZED_CLIENT, "")
return nil
}
if ret.AccessData.Client.RedirectUri == "" {
w.SetError(E_UNAUTHORIZED_CLIENT, "")
return nil
}
if ret.AccessData.IsExpired() {
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["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(time.Now()) / time.Second
if ir.AccessData.RefreshToken != "" {
w.Output["refresh_token"] = ir.AccessData.RefreshToken
}
if ir.AccessData.Scope != "" {
w.Output["scope"] = ir.AccessData.Scope
}
}