@@ -73,8 +73,46 @@ func TestSession(t *testing.T) {
73
73
})
74
74
75
75
t .Run ("case=client information reverse proxy forward" , func (t * testing.T ) {
76
+ for _ , tc := range []struct {
77
+ input string
78
+ expected string
79
+ }{
80
+ {
81
+ input : "10.10.8.1, 172.19.2.7" ,
82
+ expected : "" ,
83
+ },
84
+ {
85
+ input : "217.73.188.139,162.158.203.149, 172.19.2.7" ,
86
+ expected : "162.158.203.149" ,
87
+ },
88
+ {
89
+ input : "122.122.122.122 , 123.123.123.123" ,
90
+ expected : "123.123.123.123" ,
91
+ },
92
+ } {
93
+ t .Run ("case=parse " + tc .input , func (t * testing.T ) {
94
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
95
+ req .Header ["User-Agent" ] = []string {"Mozilla/5.0 (X11; Linux x86_64)" , "AppleWebKit/537.36 (KHTML, like Gecko)" , "Chrome/51.0.2704.103 Safari/537.36" }
96
+ req .Header .Set ("X-Forwarded-For" , tc .input )
97
+
98
+ s := session .NewInactiveSession ()
99
+ require .NoError (t , s .Activate (req , & identity.Identity {State : identity .StateActive }, conf , authAt ))
100
+ assert .True (t , s .Active )
101
+ assert .Equal (t , identity .NoAuthenticatorAssuranceLevel , s .AuthenticatorAssuranceLevel )
102
+ assert .Equal (t , authAt , s .AuthenticatedAt )
103
+ assert .Equal (t , 1 , len (s .Devices ))
104
+ assert .Equal (t , s .ID .String (), s .Devices [0 ].SessionID .String ())
105
+ assert .Equal (t , tc .expected , * s .Devices [0 ].IPAddress )
106
+ assert .Equal (t , "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" , * s .Devices [0 ].UserAgent )
107
+ assert .Equal (t , "" , * s .Devices [0 ].Location )
108
+ })
109
+ }
110
+ })
111
+
112
+ t .Run ("case=client information reverse proxy real IP set" , func (t * testing.T ) {
76
113
req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
77
114
req .Header ["User-Agent" ] = []string {"Mozilla/5.0 (X11; Linux x86_64)" , "AppleWebKit/537.36 (KHTML, like Gecko)" , "Chrome/51.0.2704.103 Safari/537.36" }
115
+ req .Header .Set ("X-Real-IP" , "54.155.246.155" )
78
116
req .Header ["X-Forwarded-For" ] = []string {"54.155.246.232" , "10.145.1.10" }
79
117
80
118
s := session .NewInactiveSession ()
@@ -84,16 +122,18 @@ func TestSession(t *testing.T) {
84
122
assert .Equal (t , authAt , s .AuthenticatedAt )
85
123
assert .Equal (t , 1 , len (s .Devices ))
86
124
assert .Equal (t , s .ID .String (), s .Devices [0 ].SessionID .String ())
87
- assert .Equal (t , "54.155.246.232" , * s .Devices [0 ].IPAddress )
125
+ assert .NotNil (t , s .Devices [0 ].UpdatedAt )
126
+ assert .NotNil (t , s .Devices [0 ].CreatedAt )
127
+ assert .Equal (t , "54.155.246.155" , * s .Devices [0 ].IPAddress )
88
128
assert .Equal (t , "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" , * s .Devices [0 ].UserAgent )
89
129
assert .Equal (t , "" , * s .Devices [0 ].Location )
90
130
})
91
131
92
- t .Run ("case=client information reverse proxy real IP set" , func (t * testing.T ) {
132
+ t .Run ("case=client information CF true client IP set" , func (t * testing.T ) {
93
133
req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
94
134
req .Header ["User-Agent" ] = []string {"Mozilla/5.0 (X11; Linux x86_64)" , "AppleWebKit/537.36 (KHTML, like Gecko)" , "Chrome/51.0.2704.103 Safari/537.36" }
95
- req .Header .Set ("X-Real -IP" , "54.155.246.155" )
96
- req .Header [ "X-Forwarded-For" ] = [] string { "54.155.246.232" , "10.145.1.10" }
135
+ req .Header .Set ("True-Client -IP" , "54.155.246.155" )
136
+ req .Header . Set ( "X-Forwarded-For" , "217.73.188.139,162.158.203.149, 172.19.2.7" )
97
137
98
138
s := session .NewInactiveSession ()
99
139
require .NoError (t , s .Activate (req , & identity.Identity {State : identity .StateActive }, conf , authAt ))
0 commit comments