|
| 1 | +package user |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/0xJacky/Nginx-UI/internal/user" |
| 10 | + "github.com/0xJacky/Nginx-UI/settings" |
| 11 | + "github.com/coreos/go-oidc/v3/oidc" |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/uozi-tech/cosy" |
| 14 | + "golang.org/x/oauth2" |
| 15 | + "gorm.io/gorm" |
| 16 | +) |
| 17 | + |
| 18 | +type OIDCLoginUser struct { |
| 19 | + Code string `json:"code" binding:"required,max=255"` |
| 20 | + State string `json:"state" binding:"required,max=255"` |
| 21 | +} |
| 22 | + |
| 23 | +func OIDCCallback(c *gin.Context) { |
| 24 | + var loginUser OIDCLoginUser |
| 25 | + |
| 26 | + ok := cosy.BindAndValid(c, &loginUser) |
| 27 | + if !ok { |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + endpoint := settings.OIDCSettings.Endpoint |
| 32 | + clientId := settings.OIDCSettings.ClientId |
| 33 | + clientSecret := settings.OIDCSettings.ClientSecret |
| 34 | + redirectUri := settings.OIDCSettings.RedirectUri |
| 35 | + |
| 36 | + if endpoint == "" || clientId == "" || clientSecret == "" || redirectUri == "" { |
| 37 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 38 | + "message": "OIDC is not configured", |
| 39 | + }) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + ctx := context.Background() |
| 44 | + provider, err := oidc.NewProvider(ctx, endpoint) |
| 45 | + if err != nil { |
| 46 | + cosy.ErrHandler(c, err) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + scopes := []string{oidc.ScopeOpenID, "profile", "email"} |
| 51 | + if settings.OIDCSettings.Scopes != "" { |
| 52 | + scopes = strings.Split(settings.OIDCSettings.Scopes, " ") |
| 53 | + } |
| 54 | + |
| 55 | + oauth2Config := oauth2.Config{ |
| 56 | + ClientID: clientId, |
| 57 | + ClientSecret: clientSecret, |
| 58 | + RedirectURL: redirectUri, |
| 59 | + Endpoint: provider.Endpoint(), |
| 60 | + Scopes: scopes, |
| 61 | + } |
| 62 | + |
| 63 | + oauth2Token, err := oauth2Config.Exchange(ctx, loginUser.Code) |
| 64 | + if err != nil { |
| 65 | + cosy.ErrHandler(c, err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + rawIDToken, ok := oauth2Token.Extra("id_token").(string) |
| 70 | + if !ok { |
| 71 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 72 | + "message": "No id_token field in oauth2 token", |
| 73 | + }) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + idTokenVerifier := provider.Verifier(&oidc.Config{ClientID: clientId}) |
| 78 | + idToken, err := idTokenVerifier.Verify(ctx, rawIDToken) |
| 79 | + if err != nil { |
| 80 | + cosy.ErrHandler(c, err) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + var claims map[string]interface{} |
| 85 | + |
| 86 | + if err := idToken.Claims(&claims); err != nil { |
| 87 | + cosy.ErrHandler(c, err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + var username string |
| 92 | + |
| 93 | + if settings.OIDCSettings.Identifier != "" { |
| 94 | + if v, ok := claims[settings.OIDCSettings.Identifier]; ok { |
| 95 | + username, _ = v.(string) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if username == "" { |
| 100 | + if v, ok := claims["email"]; ok { |
| 101 | + username, _ = v.(string) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if username == "" { |
| 106 | + if v, ok := claims["name"]; ok { |
| 107 | + username, _ = v.(string) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if username == "" { |
| 112 | + if v, ok := claims["sub"]; ok { |
| 113 | + username, _ = v.(string) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + u, err := user.GetUser(username) |
| 118 | + if err != nil { |
| 119 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 120 | + c.JSON(http.StatusForbidden, gin.H{ |
| 121 | + "message": "User not exist", |
| 122 | + }) |
| 123 | + } else { |
| 124 | + cosy.ErrHandler(c, err) |
| 125 | + } |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + userToken, err := user.GenerateJWT(u) |
| 130 | + if err != nil { |
| 131 | + cosy.ErrHandler(c, err) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + c.JSON(http.StatusOK, LoginResponse{ |
| 136 | + Message: "ok", |
| 137 | + AccessTokenPayload: userToken, |
| 138 | + }) |
| 139 | +} |
| 140 | + |
| 141 | +func GetOIDCUri(c *gin.Context) { |
| 142 | + endpoint := settings.OIDCSettings.Endpoint |
| 143 | + clientId := settings.OIDCSettings.ClientId |
| 144 | + redirectUri := settings.OIDCSettings.RedirectUri |
| 145 | + scopes := settings.OIDCSettings.Scopes |
| 146 | + |
| 147 | + if endpoint == "" || clientId == "" || redirectUri == "" { |
| 148 | + c.JSON(http.StatusOK, gin.H{ |
| 149 | + "uri": "", |
| 150 | + }) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + ctx := context.Background() |
| 155 | + provider, err := oidc.NewProvider(ctx, endpoint) |
| 156 | + if err != nil { |
| 157 | + cosy.ErrHandler(c, err) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + scopeList := []string{oidc.ScopeOpenID, "profile", "email"} |
| 162 | + if scopes != "" { |
| 163 | + scopeList = strings.Split(scopes, " ") |
| 164 | + } |
| 165 | + |
| 166 | + oauth2Config := oauth2.Config{ |
| 167 | + ClientID: clientId, |
| 168 | + RedirectURL: redirectUri, |
| 169 | + Endpoint: provider.Endpoint(), |
| 170 | + Scopes: scopeList, |
| 171 | + } |
| 172 | + |
| 173 | + state := "nginx-ui-oidc" |
| 174 | + |
| 175 | + c.JSON(http.StatusOK, gin.H{ |
| 176 | + "uri": oauth2Config.AuthCodeURL(state), |
| 177 | + }) |
| 178 | +} |
0 commit comments