@@ -15,12 +15,11 @@ import (
1515 "github.com/golang-jwt/jwt/v5"
1616 "github.com/sirupsen/logrus"
1717 "golang.org/x/oauth2"
18- "golang.org/x/oauth2/github"
1918)
2019
2120var (
22- githubOauthConfig * oauth2.Config
23- jwtSecret []byte
21+ oauthConfig * oauth2.Config
22+ jwtSecret []byte
2423)
2524
2625const oauthStateString = "random"
@@ -34,17 +33,20 @@ type AppClaims struct {
3433}
3534
3635func Init () {
37- githubOauthConfig = & oauth2.Config {
38- ClientID : os .Getenv ("GITHUB_CLIENT_ID " ),
39- ClientSecret : os .Getenv ("GITHUB_CLIENT_SECRET " ),
40- RedirectURL : os .Getenv ("GITHUB_REDIRECT_URL " ),
36+ oauthConfig = & oauth2.Config {
37+ ClientID : os .Getenv ("LINUXDO_CLIENT_ID " ),
38+ ClientSecret : os .Getenv ("LINUXDO_CLIENT_SECRET " ),
39+ RedirectURL : os .Getenv ("LINUXDO_REDIRECT_URL " ),
4140 Scopes : []string {"read:user" , "user:email" },
42- Endpoint : github .Endpoint ,
41+ Endpoint : oauth2.Endpoint {
42+ AuthURL : "https://connect.linux.do/oauth2/authorize" ,
43+ TokenURL : "https://connect.linux.do/oauth2/token" ,
44+ },
4345 }
4446 jwtSecret = []byte (os .Getenv ("JWT_SECRET" ))
4547
46- if githubOauthConfig .ClientID == "" || githubOauthConfig .ClientSecret == "" {
47- logrus .Warn ("GitHub OAuth credentials are not set. Authentication routes will not work." )
48+ if oauthConfig .ClientID == "" || oauthConfig .ClientSecret == "" {
49+ logrus .Warn ("OAuth credentials are not set. Authentication routes will not work." )
4850 }
4951 if len (jwtSecret ) == 0 {
5052 logrus .Warn ("JWT_SECRET is not set. Authentication routes will not work." )
@@ -65,72 +67,72 @@ func generateStateOauthCookie(w http.ResponseWriter) string {
6567 return state
6668}
6769
68- func HandleGitHubLogin (w http.ResponseWriter , r * http.Request ) {
69- if githubOauthConfig .ClientID == "" {
70- http .Error (w , "GitHub OAuth is not configured" , http .StatusInternalServerError )
70+ func HandleOAuthLogin (w http.ResponseWriter , r * http.Request ) {
71+ if oauthConfig .ClientID == "" {
72+ http .Error (w , "OAuth is not configured" , http .StatusInternalServerError )
7173 return
7274 }
7375 state := generateStateOauthCookie (w )
74- url := githubOauthConfig .AuthCodeURL (state )
76+ url := oauthConfig .AuthCodeURL (state )
7577 http .Redirect (w , r , url , http .StatusTemporaryRedirect )
7678}
7779
78- func HandleGitHubCallback (w http.ResponseWriter , r * http.Request ) {
79- if githubOauthConfig .ClientID == "" {
80- http .Error (w , "GitHub OAuth is not configured" , http .StatusInternalServerError )
80+ func HandleOAuthCallback (w http.ResponseWriter , r * http.Request ) {
81+ if oauthConfig .ClientID == "" {
82+ http .Error (w , "OAuth is not configured" , http .StatusInternalServerError )
8183 return
8284 }
8385
8486 oauthState , _ := r .Cookie ("oauthstate" )
8587 if r .FormValue ("state" ) != oauthState .Value {
86- logrus .Error ("invalid oauth github state" )
88+ logrus .Error ("invalid oauth state" )
8789 http .Redirect (w , r , "/" , http .StatusTemporaryRedirect )
8890 return
8991 }
9092
91- token , err := githubOauthConfig .Exchange (context .Background (), r .FormValue ("code" ))
93+ token , err := oauthConfig .Exchange (context .Background (), r .FormValue ("code" ))
9294 if err != nil {
9395 logrus .Errorf ("failed to exchange token: %s" , err .Error ())
9496 http .Redirect (w , r , "/" , http .StatusTemporaryRedirect )
9597 return
9698 }
9799
98- client := githubOauthConfig .Client (context .Background (), token )
99- resp , err := client .Get ("https://api.github.com /user" )
100+ client := oauthConfig .Client (context .Background (), token )
101+ resp , err := client .Get ("https://connect.linux.do/api /user" )
100102 if err != nil {
101- logrus .Errorf ("failed to get user from github : %s" , err .Error ())
103+ logrus .Errorf ("failed to get user from oauth provider : %s" , err .Error ())
102104 http .Redirect (w , r , "/" , http .StatusTemporaryRedirect )
103105 return
104106 }
105107 defer resp .Body .Close ()
106108
107109 body , err := io .ReadAll (resp .Body )
108110 if err != nil {
109- logrus .Errorf ("failed to read github response body: %s" , err .Error ())
111+ logrus .Errorf ("failed to read oauth provider response body: %s" , err .Error ())
110112 http .Redirect (w , r , "/" , http .StatusTemporaryRedirect )
111113 return
112114 }
113115
114- var githubUser struct {
116+ var oauthUser struct {
115117 ID int64 `json:"id"`
116118 Login string `json:"login"`
117119 AvatarURL string `json:"avatar_url"`
118120 Name string `json:"name"`
119121 }
120122
121- if err := json .Unmarshal (body , & githubUser ); err != nil {
122- logrus .Errorf ("failed to unmarshal github user: %s" , err .Error ())
123+ if err := json .Unmarshal (body , & oauthUser ); err != nil {
124+ logrus .Errorf ("failed to unmarshal oauth user: %s" , err .Error ())
123125 http .Redirect (w , r , "/" , http .StatusTemporaryRedirect )
124126 return
125127 }
126128
127129 // For now we don't have a user database, so we create a user object on the fly.
128130 // In phase 3, we will save/get the user from the database here.
129131 user := & core.User {
130- GitHubID : githubUser .ID ,
131- Login : githubUser .Login ,
132- AvatarURL : githubUser .AvatarURL ,
133- Name : githubUser .Name ,
132+ GitHubID : oauthUser .ID ,
133+ Login : oauthUser .Login ,
134+ AvatarURL : oauthUser .AvatarURL ,
135+ Name : oauthUser .Name ,
134136 }
135137
136138 jwtToken , err := createJWT (user )
0 commit comments