-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rework to apply standard hierarchy - move auth to dedicated sub package - move header to dedicated sub package Package Static: - fix bugs
- Loading branch information
Showing
21 changed files
with
668 additions
and
383 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
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,48 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package auth | ||
|
||
import ( | ||
ginsdk "github.com/gin-gonic/gin" | ||
liberr "github.com/nabbar/golib/errors" | ||
liblog "github.com/nabbar/golib/logger" | ||
rtrhdr "github.com/nabbar/golib/router/authheader" | ||
) | ||
|
||
type Authorization interface { | ||
Handler(c *ginsdk.Context) | ||
Register(router ...ginsdk.HandlerFunc) ginsdk.HandlerFunc | ||
Append(router ...ginsdk.HandlerFunc) | ||
} | ||
|
||
func NewAuthorization(log liblog.FuncLog, HeadAuthType string, authCheckFunc func(AuthHeader string) (rtrhdr.AuthCode, liberr.Error)) Authorization { | ||
return &authorization{ | ||
log: log, | ||
check: authCheckFunc, | ||
authType: HeadAuthType, | ||
router: make([]ginsdk.HandlerFunc, 0), | ||
} | ||
} |
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,68 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package authheader | ||
|
||
import ( | ||
"net/http" | ||
|
||
ginsdk "github.com/gin-gonic/gin" | ||
) | ||
|
||
type AuthCode uint8 | ||
|
||
const ( | ||
AuthCodeSuccess = iota | ||
AuthCodeRequire | ||
AuthCodeForbidden | ||
) | ||
|
||
const ( | ||
HeaderAuthRequire = "WWW-Authenticate" | ||
HeaderAuthSend = "Authorization" | ||
HeaderAuthReal = "Basic realm=LDAP Authorization Required" | ||
) | ||
|
||
func AuthRequire(c *ginsdk.Context, err error) { | ||
if err != nil { | ||
c.Errors = append(c.Errors, &ginsdk.Error{ | ||
Err: err, | ||
Type: ginsdk.ErrorTypePrivate, | ||
}) | ||
} | ||
// Credentials doesn't match, we return 401 and abort handlers chain. | ||
c.Header(HeaderAuthRequire, HeaderAuthReal) | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
} | ||
|
||
func AuthForbidden(c *ginsdk.Context, err error) { | ||
if err != nil { | ||
c.Errors = append(c.Errors, &ginsdk.Error{ | ||
Err: err, | ||
Type: ginsdk.ErrorTypePrivate, | ||
}) | ||
} | ||
c.AbortWithStatus(http.StatusForbidden) | ||
} |
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,109 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package router | ||
|
||
import ( | ||
"net/http" | ||
|
||
ginsdk "github.com/gin-gonic/gin" | ||
) | ||
|
||
func DefaultGinInit() *ginsdk.Engine { | ||
engine := ginsdk.New() | ||
engine.Use(ginsdk.Logger(), ginsdk.Recovery()) | ||
|
||
return engine | ||
} | ||
|
||
func DefaultGinWithTrustyProxy(trustyProxy []string) *ginsdk.Engine { | ||
engine := ginsdk.New() | ||
engine.Use(ginsdk.Logger(), ginsdk.Recovery()) | ||
|
||
if len(trustyProxy) > 0 { | ||
_ = engine.SetTrustedProxies(trustyProxy) | ||
} | ||
|
||
return engine | ||
} | ||
|
||
func DefaultGinWithTrustedPlatform(trustedPlatform string) *ginsdk.Engine { | ||
engine := ginsdk.New() | ||
engine.Use(ginsdk.Logger(), ginsdk.Recovery()) | ||
|
||
if len(trustedPlatform) > 0 { | ||
engine.TrustedPlatform = trustedPlatform | ||
} | ||
|
||
return engine | ||
} | ||
|
||
func RoutersRegister(method string, relativePath string, router ...ginsdk.HandlerFunc) { | ||
defaultRouters.Register(method, relativePath, router...) | ||
} | ||
|
||
func RoutersRegisterInGroup(group, method string, relativePath string, router ...ginsdk.HandlerFunc) { | ||
defaultRouters.RegisterInGroup(group, method, relativePath, router...) | ||
} | ||
|
||
func RoutersHandler(engine *ginsdk.Engine) { | ||
defaultRouters.Handler(engine) | ||
} | ||
|
||
func GinEngine(trustedPlatform string, trustyProxy ...string) (*ginsdk.Engine, error) { | ||
var err error | ||
|
||
engine := ginsdk.New() | ||
if len(trustyProxy) > 0 { | ||
err = engine.SetTrustedProxies(trustyProxy) | ||
} | ||
if len(trustedPlatform) > 0 { | ||
engine.TrustedPlatform = trustedPlatform | ||
} | ||
|
||
return engine, err | ||
} | ||
|
||
func GinAddGlobalMiddleware(eng *ginsdk.Engine, middleware ...ginsdk.HandlerFunc) *ginsdk.Engine { | ||
eng.Use(middleware...) | ||
return eng | ||
} | ||
|
||
// SetGinHandler func that return given func as ginTonic HandlerFunc interface type. | ||
func SetGinHandler(fct func(c *ginsdk.Context)) ginsdk.HandlerFunc { | ||
return fct | ||
} | ||
|
||
func Handler(routerList RouterList) http.Handler { | ||
e := routerList.Engine() | ||
|
||
if routerList == nil { | ||
RoutersHandler(e) | ||
} else { | ||
routerList.Handler(e) | ||
} | ||
|
||
return e | ||
} |
Oops, something went wrong.