Commit 94fa8fb1 authored by yzx9's avatar yzx9
Browse files

Add OAuth2 authorization content type configuration

parent 40eb01cc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ services:
      # OAUTH2_CLIENT_SECRET: YOUR_OAUTH2_CLIENT_SECRET
      # OAUTH2_SCOPE: YOUR_OAUTH2_SCOPE
      # OAUTH2_AUTHORIZATION_URL: YOUR_OAUTH2_AUTHORIZATION_URL
      # OAUTH2_AUTHORIZATION_CONTENT_TYPE: # One of ['application/x-www-form-urlencoded', 'application/json']
      # OAUTH2_TOKEN_URL: YOUR_OAUTH2_TOKEN_URL
      # OAUTH2_PROFILE_URL: YOUR_OAUTH2_PROFILE_URL
      # OAUTH2_USER_ATTR_EMAIL: email
+1 −0
Original line number Diff line number Diff line
@@ -169,6 +169,7 @@ services:
      # OAUTH2_CLIENT_SECRET: YOUR_OAUTH2_CLIENT_SECRET
      # OAUTH2_SCOPE: YOUR_OAUTH2_SCOPE
      # OAUTH2_AUTHORIZATION_URL: YOUR_OAUTH2_AUTHORIZATION_URL
      # OAUTH2_AUTHORIZATION_CONTENT_TYPE: # One of ['application/x-www-form-urlencoded', 'application/json']
      # OAUTH2_TOKEN_URL: YOUR_OAUTH2_TOKEN_URL
      # OAUTH2_PROFILE_URL: YOUR_OAUTH2_PROFILE_URL
      # OAUTH2_USER_ATTR_EMAIL: email
+1 −0
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ services:
      # OAUTH2_CLIENT_SECRET: YOUR_OAUTH2_CLIENT_SECRET
      # OAUTH2_SCOPE: YOUR_OAUTH2_SCOPE
      # OAUTH2_AUTHORIZATION_URL: YOUR_OAUTH2_AUTHORIZATION_URL
      # OAUTH2_AUTHORIZATION_CONTENT_TYPE: # One of ['application/x-www-form-urlencoded', 'application/json']
      # OAUTH2_TOKEN_URL: YOUR_OAUTH2_TOKEN_URL
      # OAUTH2_PROFILE_URL: YOUR_OAUTH2_PROFILE_URL
      # OAUTH2_USER_ATTR_EMAIL: email
+17 −13
Original line number Diff line number Diff line
@@ -292,28 +292,33 @@ const AuthenticationController = {
  },

  async oauth2Callback(req, res, next) {
    console.log(`OAuth, receive code ${req.query.code} and state ${req.query.state}`)
    const saveState = req.session.oauth2State
    delete req.session.oauth2State
    if (saveState !== req.query.state) {
      console.log("OAuth ", JSON.stringify(user))
      return AuthenticationController.finishLogin(false, req, res, next)
    }

    try {
      console.log("OAuth2 code", req.query.code)
      const tokenResponse = await fetch(process.env.OAUTH2_TOKEN_URL, {
        method: 'POST',
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
      const contentType = process.env.OAUTH2_AUTHORIZATION_CONTENT_TYPE || 'application/x-www-form-urlencoded'
      const bodyParams = {
        grant_type: "authorization_code",
        client_id: process.env.OAUTH2_CLIENT_ID,
        client_secret: process.env.OAUTH2_CLIENT_SECRET,
        code: req.query.code,
        redirect_uri: `${process.env.SHARELATEX_SITE_URL}/oauth/callback`,
        })
      }
      const body = contentType === 'application/json'
        ? JSON.stringify(bodyParams)
        : new URLSearchParams(bodyParams).toString()

      const tokenResponse = await fetch(process.env.OAUTH2_TOKEN_URL, {
        method: 'POST',
        headers: {
          "Accept": "application/json",
          "Content-Type": contentType,
        },
        body
      })
      
      const tokenData = await tokenResponse.json()
@@ -324,9 +329,8 @@ const AuthenticationController = {
        headers: {
          "Accept": "application/json",
          "Authorization": `Bearer ${tokenData.access_token}`,
          "Content-Type": "application/json",
        }
      })
      });
      const profile = await profileResponse.json()
      console.log("OAuth2 user profile", JSON.stringify(profile))