Skip to content

Commit

Permalink
Add Qirikki Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Azimjon Pulatov committed Jun 28, 2024
1 parent cfb212b commit 57370f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions server/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type User struct {
ID int `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Allowed bool `json:"allowed"`
JoinedDate string `json:"created_at"`
}

Expand All @@ -28,6 +29,7 @@ type github struct {
clientSecret string
defaultScope string
userEndpoint string
qir2Endpoint string
redirectUri string
}

Expand All @@ -38,6 +40,7 @@ func New(clientId, clientSecret string) Authenticator {
defaultScope: "user:email",
userEndpoint: "https://api.github.com/user",
redirectUri: "https://jprq.io/oauth-callback",
qir2Endpoint: "https://api.42.uz/api/profile/jprq/",
}
}

Expand Down Expand Up @@ -84,11 +87,19 @@ func (g github) ObtainToken(code string) (string, error) {
}

func (g github) Authenticate(token string) (User, error) {
user, err := g.authenticate(g.userEndpoint, fmt.Sprintf("token %s%s", tokenPrefix, token))
if err != nil {
user, err = g.authenticate(g.qir2Endpoint, fmt.Sprintf("Token %s", token))
}
return user, err
}

func (g github) authenticate(endpoint, token string) (User, error) {
user := User{}
client := &http.Client{}

req, _ := http.NewRequest("GET", g.userEndpoint, nil)
req.Header.Set("Authorization", fmt.Sprintf("token %s%s", tokenPrefix, token))
req, _ := http.NewRequest("GET", endpoint, nil)
req.Header.Set("Authorization", token)
resp, err := client.Do(req)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/jprq.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (j *Jprq) serveEventConn(conn net.Conn) error {
return events.WriteError(conn, "authentication failed %s", "\n\tobtain auth token from https://jprq.io/auth\n")
}

if _, found := j.allowedUsers[user.Login]; !found {
if _, found := j.allowedUsers[user.Login]; !found && !user.Allowed {
return events.WriteError(conn, "jprq is now invite-only service %s\n", "\n\tbuy membership - https://buymeacoffee.com/azimjon \n")
}
if len(j.userTunnels[user.Login]) >= j.config.MaxTunnelsPerUser {
Expand Down

0 comments on commit 57370f3

Please sign in to comment.