@@ -35,6 +35,7 @@ import java.nio.charset.Charset
35
35
import java.nio.file.Files
36
36
import java.nio.file.NoSuchFileException
37
37
import java.nio.file.Path
38
+ import java.security.MessageDigest
38
39
import java.time.LocalDateTime
39
40
import java.time.format.DateTimeFormatter
40
41
import java.util.*
@@ -43,6 +44,7 @@ import java.util.concurrent.TimeUnit
43
44
import java.util.function.Consumer
44
45
import java.util.stream.Collectors
45
46
import java.util.stream.Stream
47
+ import kotlin.math.sign
46
48
47
49
@Service
48
50
class CourseServiceForCaching (
554
556
}
555
557
556
558
@Transactional
557
- fun webhookUpdateCourse (courseSlug : String , secret : String ): Course ? {
559
+ fun webhookUpdateWithSecret (courseSlug : String , secret : String? ): Course ? {
558
560
val existingCourse = getCourseBySlug(courseSlug)
559
- if (existingCourse.webhookSecret != null && existingCourse.webhookSecret == secret) {
560
- return updateCourse(courseSlug)
561
+ if (existingCourse.webhookSecret != null && secret != null ) {
562
+ if (existingCourse.webhookSecret == secret) {
563
+ return updateCourse(courseSlug)
564
+ }
565
+ }
566
+ logger.debug { " Provided webhook secret does not match secret of course $courseSlug " }
567
+ throw ResponseStatusException (HttpStatus .FORBIDDEN )
568
+ }
569
+
570
+ fun webhookUpdateWithSignature (courseSlug : String , signature : String? ): Course ? {
571
+ val existingCourse = getCourseBySlug(courseSlug)
572
+ if (existingCourse.webhookSecret != null && signature != null ) {
573
+ val expected = MessageDigest .getInstance(" SHA-256" )
574
+ .digest(existingCourse.webhookSecret!! .toByteArray())
575
+ .fold(" " ) { str, it -> str + " %02x" .format(it) }
576
+ logger.info{" Expected: ${expected} based on secret ${existingCourse.webhookSecret} , actual: ${signature} " }
577
+ if (expected == signature) {
578
+ return updateCourse(courseSlug)
579
+ }
561
580
}
562
581
logger.debug { " Provided webhook secret does not match secret of course $courseSlug " }
563
582
throw ResponseStatusException (HttpStatus .FORBIDDEN )
0 commit comments