All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fortysevendeg.intools.jwt.utils.JWTUtils.scala Maven / Gradle / Ivy

There is a newer version: 0.0.13
Show newest version
/*
 * Copyright 2022 47 Degrees 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fortysevendeg.intools.jwt.utils

import cats.implicits.*
import io.circe.parser.parse
import pdi.jwt.{JwtBase64, JwtUtils}
import com.fortysevendeg.intools.jwt.models.Types.{JWTClaim, JWTGoogleToken}
import com.fortysevendeg.intools.jwt.models.{JWT, JWTError, JWTGoogleUser, JWTTokenRaw}

object JWTUtils:

  private[jwt] val getClaim: JWTTokenRaw => JWTClaim = _.jwtClaim

  private[jwt] val decodeString: JWTClaim => String = c => JwtBase64.decodeString(c.value)

  private[jwt] val parseJson: String => Either[JWTError, JWT] = parse(_).flatMap(_.as[JWT]).leftMap(e => JWTError.JWTParsingError(e.getMessage))

  val getJWT: JWTTokenRaw => Either[JWTError, JWT] = (getClaim andThen decodeString andThen parseJson)(_)

  val getJwtFromString: String => Either[JWTError, JWT] = JWTTokenRaw.fromString(_).fold(Left(JWTError.MalformedJWT))(getJWT)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy