-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9678 from reasonerjt/refine-req-handle-1.9
Refine request handle process
- Loading branch information
Showing
19 changed files
with
205 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ enablegzip = true | |
|
||
[dev] | ||
httpport = 8080 | ||
EnableXSRF = true | ||
XSRFKey = {{xsrf_key}} | ||
XSRFExpire = 3600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package filter | ||
|
||
import ( | ||
"context" | ||
beegoctx "github.com/astaxie/beego/context" | ||
"github.com/goharbor/harbor/src/common/utils/log" | ||
"github.com/goharbor/harbor/src/core/config" | ||
"net/http" | ||
) | ||
|
||
// SessionReqKey is the key in the context of a request to mark the request carries session when reaching the backend | ||
const SessionReqKey ContextValueKey = "harbor_with_session_req" | ||
|
||
// SessionCheck is a filter to mark the requests that carries a session id, it has to be registered as | ||
// "beego.BeforeStatic" because beego will modify the request after execution of these filters, all requests will | ||
// appear to have a session id cookie. | ||
func SessionCheck(ctx *beegoctx.Context) { | ||
req := ctx.Request | ||
_, err := req.Cookie(config.SessionCookieName) | ||
if err == nil { | ||
ctx.Request = req.WithContext(context.WithValue(req.Context(), SessionReqKey, true)) | ||
log.Debug("Mark the request as no-session") | ||
} | ||
} | ||
|
||
// ReqCarriesSession verifies if the request carries session when | ||
func ReqCarriesSession(req *http.Request) bool { | ||
r, ok := req.Context().Value(SessionReqKey).(bool) | ||
return ok && r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package filter | ||
|
||
import ( | ||
beegoctx "github.com/astaxie/beego/context" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestReqHasNoSession(t *testing.T) { | ||
req, _ := http.NewRequest("POST", "https://127.0.0.1:8080/api/users", nil) | ||
ctx := beegoctx.NewContext() | ||
ctx.Request = req | ||
SessionCheck(ctx) | ||
assert.False(t, ReqCarriesSession(ctx.Request)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/portal/lib/src/service/http-xsrf-token-extractor.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
|
||
import { HttpXsrfTokenExtractorToBeUsed } from './http-xsrf-token-extractor.service'; | ||
import { SharedModule } from '../shared/shared.module'; | ||
import { CookieService } from "ngx-cookie"; | ||
|
||
describe('HttpXsrfTokenExtractorToBeUsed', () => { | ||
let cookie = "fdsa|ds"; | ||
let mockCookieService = { | ||
get: function () { | ||
return cookie; | ||
}, | ||
set: function (cookieStr: string) { | ||
cookie = cookieStr; | ||
} | ||
}; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
SharedModule | ||
], | ||
providers: [ | ||
HttpXsrfTokenExtractorToBeUsed, | ||
{ provide: CookieService, useValue: mockCookieService} | ||
] | ||
}); | ||
|
||
}); | ||
|
||
it('should be initialized', inject([HttpXsrfTokenExtractorToBeUsed], (service: HttpXsrfTokenExtractorToBeUsed) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
|
||
it('should be get right token when the cookie exists', inject([HttpXsrfTokenExtractorToBeUsed], | ||
(service: HttpXsrfTokenExtractorToBeUsed) => { | ||
mockCookieService.set("fdsa|ds"); | ||
let token = service.getToken(); | ||
expect(btoa(token)).toEqual(cookie.split("|")[0]); | ||
})); | ||
|
||
it('should be get right token when the cookie does not exist', inject([HttpXsrfTokenExtractorToBeUsed], | ||
(service: HttpXsrfTokenExtractorToBeUsed) => { | ||
mockCookieService.set(null); | ||
let token = service.getToken(); | ||
expect(token).toBeNull(); | ||
})); | ||
|
||
|
||
}); |
18 changes: 18 additions & 0 deletions
18
src/portal/lib/src/service/http-xsrf-token-extractor.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { HttpXsrfTokenExtractor } from "@angular/common/http"; | ||
import { CookieService } from "ngx-cookie"; | ||
@Injectable() | ||
export class HttpXsrfTokenExtractorToBeUsed extends HttpXsrfTokenExtractor { | ||
constructor( | ||
private cookieService: CookieService, | ||
) { | ||
super(); | ||
} | ||
public getToken(): string | null { | ||
const csrfCookie = this.cookieService.get("_xsrf"); | ||
if (csrfCookie) { | ||
return atob(csrfCookie.split("|")[0]); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.