/
prompt.go
40 lines (36 loc) · 1.15 KB
/
prompt.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
package oauth
import (
"github.com/authgear/authgear-server/pkg/lib/oauth/protocol"
"github.com/authgear/authgear-server/pkg/lib/session"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/slice"
)
type PromptResolver struct {
Clock clock.Clock
}
func (r *PromptResolver) ResolvePrompt(req protocol.AuthorizationRequest, sidSession session.Session) (prompt []string) {
prompt = req.Prompt()
if maxAge, ok := req.MaxAge(); ok {
impliesPromptLogin := false
// When there is no session, the presence of max_age implies prompt=login.
if sidSession == nil {
impliesPromptLogin = true
} else {
// max_age=0 implies prompt=login
if maxAge == 0 {
impliesPromptLogin = true
} else {
// max_age=n implies prompt=login if elapsed time is greater than max_age.
// In extreme rare case, elapsed time can be negative.
elapsedTime := r.Clock.NowUTC().Sub(sidSession.GetAuthenticationInfo().AuthenticatedAt)
if elapsedTime < 0 || elapsedTime > maxAge {
impliesPromptLogin = true
}
}
}
if impliesPromptLogin {
prompt = slice.AppendIfUniqueStrings(prompt, "login")
}
}
return
}